标签:
折半搜索,先搜索一半的数字,记录第一个人的值,第二个人、第三个人和第一个人的差值,开个map哈希存一下,然后另一半搜完直接根据差值查找前一半的答案。
代码
1 #include<cstdio> 2 #include<map> 3 #define ll long long 4 #define N 100 5 using namespace std; 6 map<long long,int> ma,Ma; 7 char str[3]; 8 int n,i,a[N],b[N],c[N],Ans,A1,A2,ans[N]; 9 void dfs(ll x,ll y,ll z,int d,int k) 10 { 11 if (d>n/2) 12 { 13 long long tmp=(y-x)*300007+(z-x); 14 if ((ma.find(tmp)==ma.end())||(x>ma[tmp])) 15 { 16 ma[tmp]=x; 17 Ma[tmp]=k; 18 } 19 return; 20 } 21 dfs(x+a[d],y+b[d],z,d+1,k*3); 22 dfs(x,y+b[d],z+c[d],d+1,k*3+1); 23 dfs(x+a[d],y,z+c[d],d+1,k*3+2); 24 } 25 void Dfs(ll x,ll y,ll z,int d,int k) 26 { 27 if (d>n) 28 { 29 long long tmp=(x-y)*300007+(x-z); 30 if (ma.find(tmp)!=ma.end()) 31 { 32 if (ma[tmp]+x>Ans) 33 { 34 Ans=ma[tmp]+x; 35 A1=Ma[tmp]; 36 A2=k; 37 } 38 } 39 return; 40 } 41 Dfs(x+a[d],y+b[d],z,d+1,k*3); 42 Dfs(x,y+b[d],z+c[d],d+1,k*3+1); 43 Dfs(x+a[d],y,z+c[d],d+1,k*3+2); 44 } 45 int main() 46 { 47 scanf("%d",&n); 48 Ans=-0x37373737;int q=0; 49 for (i=1;i<=n;i++) 50 { 51 scanf("%d%d%d",&a[i],&b[i],&c[i]); 52 } 53 /* 54 for (i=1;i<=n;i++) 55 { 56 scanf("%s",str); 57 if (str[0]==‘L‘) q+=a[i]; 58 } 59 printf("%d\n",q); 60 */ 61 dfs(0,0,0,1,0); 62 Dfs(0,0,0,n/2+1,0); 63 64 if (Ans==-0x37373737) 65 printf("Impossible"); 66 else 67 { 68 //printf("%d\n",Ans); 69 for (i=n;i>n/2;i--) 70 { 71 ans[i]=A2%3; 72 A2=A2/3; 73 } 74 for (i=n/2;i>=1;i--) 75 { 76 ans[i]=A1%3; 77 A1=A1/3; 78 } 79 for (i=1;i<=n;i++) 80 if (ans[i]==0) 81 printf("LM\n"); 82 else 83 if (ans[i]==1) 84 printf("MW\n"); 85 else 86 if (ans[i]==2) 87 printf("LW\n"); 88 } 89 }
Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning
标签:
原文地址:http://www.cnblogs.com/fzmh/p/4878724.html