[关闭]
@Alpacadh 2019-02-19T15:01:39.000000Z 字数 4999 阅读 630

D、E、F

搜索


D - Mobile Computing

题意:

解题思路:

代码:

  1. #include<bits/stdc++.h>
  2. //#include<iostream>
  3. //#include<algorithm>
  4. //#include<cmath>
  5. //#include<cstring>
  6. //#include<stdio.h>
  7. using namespace std;
  8. const int inf=0x3f3f3f3f;
  9. const int N=1e4+5;
  10. const double eps=1e-4;
  11. typedef long long ll;
  12. typedef pair<int,ll> pil;
  13. struct node
  14. {
  15. double l,r;
  16. }t[N];
  17. int vis[N];
  18. double w[N];
  19. double ans;
  20. double r;
  21. int n;
  22. int len;
  23. void dfs(int x)
  24. {
  25. if(x==n-1)
  26. {
  27. if(t[len].l+t[len].r-r<eps)
  28. ans=max(ans,t[len].l+t[len].r);
  29. return;
  30. }
  31. for(int i=1;i<=len;i++)
  32. {
  33. if(!vis[i])
  34. {
  35. for(int j=1;j<=len;j++)
  36. {
  37. if(!vis[j]&&i!=j)
  38. {
  39. vis[i]=1,vis[j]=1;
  40. len++;
  41. w[len]=w[i]+w[j];
  42. double dl=w[j]/(w[i]+w[j]),dr=1.0-dl;
  43. t[len].l=max(t[i].l+dl,t[j].l-dr);
  44. t[len].r=max(t[j].r+dr,t[i].r-dl);
  45. dfs(x+1);
  46. vis[i]=0,vis[j]=0,vis[len]=0;
  47. t[len].l=0,t[len].r=0;
  48. len--;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. int main()
  55. {
  56. int T;
  57. scanf("%d",&T);
  58. while(T--)
  59. {
  60. memset(t,0,sizeof(t));
  61. memset(w,0,sizeof(w));
  62. memset(vis,0,sizeof(vis));
  63. scanf("%lf%d",&r,&n);
  64. for(int i=1;i<=n;i++)
  65. {
  66. scanf("%lf",&w[i]);
  67. }
  68. len=n;
  69. ans=-1;
  70. dfs(0);
  71. if(ans==-1)
  72. printf("%.f\n",ans);
  73. else
  74. printf("%.8f\n",ans);
  75. }
  76. return 0;
  77. }

E - The Morning after Halloween

题意:

解题思路:

代码:

(自己写的太复杂了,贴一下别人hash过的代码)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <queue>
  6. using namespace std;
  7. const int maxs = 20;
  8. const int maxn = 150;
  9. int n,m,cnt,sum;
  10. int deg[maxn],G[maxn][maxn];
  11. int x[maxn],y[maxn],id[maxn][maxn];
  12. int d[maxn][maxn][maxn];
  13. int vis[maxn][maxn][maxn];
  14. int s[5],t[5];
  15. int dx[] = {0,0,0,-1,1};
  16. int dy[] = {0,1,-1,0,0};
  17. int find_ID(int a,int b,int c){
  18. return (a<<16)|(b<<8)|c;
  19. }//hash操作,将三位数变成一个整数。
  20. bool conflict(int a,int b,int a2,int b2){
  21. if((a2==b && a==b2) || a2==b2) return true;
  22. return false;
  23. }
  24. int bfs(){
  25. queue<int> que,anti_que;
  26. d[s[0]][s[1]][s[2]] = 0;
  27. d[t[0]][t[1]][t[2]] = 0;
  28. que.push(find_ID(s[0],s[1],s[2]));
  29. anti_que.push(find_ID(t[0],t[1],t[2]));
  30. vis[s[0]][s[1]][s[2]] = 1;
  31. vis[t[0]][t[1]][t[2]] = 2;
  32. while(!que.empty() && !anti_que.empty()){
  33. int num = que.size(),anti_num = anti_que.size();
  34. while(num--){
  35. int top = que.front();que.pop();
  36. int a = (top>>16)&0xff,b = (top>>8)&0xff,c = (top&0xff);
  37. for(int i = 0;i < deg[a];i++){
  38. int a2 = G[a][i];
  39. for(int j = 0;j < deg[b];j++){
  40. int b2 = G[b][j];
  41. if(conflict(a,b,a2,b2)) continue;
  42. for(int k = 0;k < deg[c];k++){
  43. int c2 = G[c][k];
  44. if(conflict(a,c,a2,c2)) continue;
  45. if(conflict(b,c,b2,c2)) continue;
  46. if(vis[a2][b2][c2] == 0){
  47. d[a2][b2][c2] = d[a][b][c]+1;
  48. vis[a2][b2][c2] = 1;
  49. que.push(find_ID(a2,b2,c2));
  50. }
  51. else if(vis[a2][b2][c2] == 2){
  52. return d[a][b][c]+d[a2][b2][c2]+1;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. while(anti_num--){
  59. int top = anti_que.front();anti_que.pop();
  60. int a = (top>>16)&0xff,b = (top>>8)&0xff,c = (top&0xff);
  61. for(int i = 0;i < deg[a];i++){
  62. int a2 = G[a][i];
  63. for(int j = 0;j < deg[b];j++){
  64. int b2 = G[b][j];
  65. if(conflict(a,b,a2,b2)) continue;
  66. for(int k = 0;k < deg[c];k++){
  67. int c2 = G[c][k];
  68. if(conflict(a,c,a2,c2)) continue;
  69. if(conflict(b,c,b2,c2)) continue;
  70. if(vis[a2][b2][c2] == 0){
  71. d[a2][b2][c2] = d[a][b][c]+1;
  72. vis[a2][b2][c2] = 2;
  73. anti_que.push(find_ID(a2,b2,c2));
  74. }
  75. else if(vis[a2][b2][c2] == 1){
  76. return d[a][b][c]+d[a2][b2][c2]+1;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. return -1;
  84. }
  85. int main()
  86. {
  87. //freopen("input.txt","r",stdin);
  88. //freopen("output.txt","w",stdout);
  89. while(~scanf("%d%d%d\n",&m,&n,&sum) && (m||n||sum)){
  90. char gra[maxs][maxs];
  91. cnt = 0;
  92. memset(d,-1,sizeof(d));
  93. memset(vis,0,sizeof(vis));
  94. for(int i = 0;i < n;i++) fgets(gra[i],maxs,stdin);
  95. for(int i = 0;i < n;i++){
  96. for(int j = 0;j < m;j++){
  97. if(gra[i][j] != '#'){
  98. x[cnt] = i,y[cnt] = j,id[i][j] = cnt;
  99. if(islower(gra[i][j])) s[gra[i][j]-'a'] = cnt;
  100. else if(isupper(gra[i][j])) t[gra[i][j]-'A'] = cnt;
  101. cnt++;
  102. }
  103. }
  104. }
  105. for(int i = 0;i < cnt;i++){
  106. int X = x[i],Y = y[i];
  107. deg[i] = 0;
  108. for(int k = 0;k < 5;k++){
  109. int xx = X+dx[k],yy = Y+dy[k];
  110. if(gra[xx][yy] != '#') G[i][deg[i]++] = id[xx][yy];
  111. }
  112. }
  113. if(sum <= 2){
  114. s[2] = t[2] = G[cnt][0] = cnt;
  115. deg[cnt++] = 1;
  116. }
  117. if(sum <= 1){
  118. s[1] = t[1] = G[cnt][0] = cnt;
  119. deg[cnt++] = 1;
  120. }
  121. printf("%d\n",bfs());
  122. }
  123. return 0;
  124. }

F - Editing a Book

题意:

解题思路:

代码:

  1. //#include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<cstring>
  6. #include<stdio.h>
  7. #include<cstdlib>
  8. #include<cstdio>
  9. using namespace std;
  10. const int inf=0x3f3f3f3f;
  11. const int N=10;
  12. const double eps=1e-4;
  13. typedef long long ll;
  14. typedef pair<int,ll> pil;
  15. int a[N];
  16. int n;
  17. bool check()
  18. {
  19. for(int i=0;i<n-1;i++)
  20. if(a[i]>a[i+1])
  21. return false;
  22. return true;
  23. }
  24. int h()
  25. {
  26. int cnt=0;
  27. for(int i=0;i<n-1;i++)
  28. {
  29. if(a[i]+1!=a[i+1])
  30. cnt++;
  31. }
  32. if(a[n-1]!=n)
  33. cnt++;
  34. return cnt;
  35. }
  36. int dfs(int d,int dep)
  37. {
  38. if(3*d+h()>3*dep)
  39. return 0;
  40. if(check())
  41. return 1;
  42. int b[N],tep[N];
  43. memcpy(tep,a,sizeof(a));
  44. for(int i=0;i<n;i++)
  45. {
  46. for(int j=i;j<n;j++)
  47. {
  48. int cnt=0;
  49. for(int k=0;k<n;k++)
  50. {
  51. if(k<i||k>j)
  52. {
  53. b[cnt++]=a[k];
  54. }
  55. }
  56. for(int k=0;k<=cnt;k++)
  57. {
  58. int cnt1=0;
  59. for(int kk=0;kk<k;kk++)
  60. a[cnt1++]=b[kk];
  61. for(int kk=i;kk<=j;kk++)
  62. a[cnt1++]=tep[kk];
  63. for(int kk=k;kk<cnt;kk++)
  64. a[cnt1++]=b[kk];
  65. if(dfs(d+1,dep))
  66. return 1;
  67. memcpy(a,tep,sizeof(tep));
  68. }
  69. }
  70. }
  71. return 0;
  72. }
  73. int main()
  74. {
  75. int tot=1;
  76. while(scanf("%d",&n))
  77. {
  78. if(n==0)
  79. break;
  80. for(int i=0;i<n;i++)
  81. scanf("%d",&a[i]);
  82. int ans=5;
  83. if(check())
  84. ans=0;
  85. else
  86. {
  87. for(int i=1;i<5;i++)
  88. {
  89. if(dfs(0,i))
  90. {
  91. ans=i;
  92. break;
  93. }
  94. }
  95. }
  96. printf("Case %d: %d\n",tot++,ans);
  97. }
  98. return 0;
  99. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注