Binary search, first search half of the number, record the first person's value, the second person, the third person and the first person's difference, open a map Hachicun, then the other half search directly based on the difference to find the first half of the answer.
Code
1#include <cstdio>2#include <map>3 #definell Long Long4 #defineN 1005 using namespacestd;6map<Long Long,int>Ma,ma;7 Charstr[3];8 intN,i,a[n],b[n],c[n],ans,a1,a2,ans[n];9 voidDFS (ll x,ll y,ll Z,intDintk)Ten { One if(d>n/2) A { - Long Longtmp= (y-x) *300007+ (Z-x); - if((Ma.find (TMP) ==ma.end ()) | | (x>ma[tmp])) the { -ma[tmp]=x; -ma[tmp]=K; - } + return; - } +DFS (x+a[d],y+b[d],z,d+1, k*3); ADFS (x,y+b[d],z+c[d],d+1, k*3+1); atDFS (x+a[d],y,z+c[d],d+1, k*3+2); - } - voidDfs (ll x,ll y,ll Z,intDintk) - { - if(d>N) - { in Long Longtmp= (x-y) *300007+ (X-z); - if(Ma.find (tmp)! =ma.end ()) to { + if(ma[tmp]+x>Ans) - { theans=ma[tmp]+x; *a1=ma[tmp]; $A2=K;Panax Notoginseng } - } the return; + } ADfs (x+a[d],y+b[d],z,d+1, k*3); theDfs (x,y+b[d],z+c[d],d+1, k*3+1); +Dfs (x+a[d],y,z+c[d],d+1, k*3+2); - } $ intMain () $ { -scanf"%d",&n); -ans=-0x37373737;intq=0; the for(i=1; i<=n;i++) - {Wuyiscanf"%d%d%d",&a[i],&b[i],&c[i]); the } - /* Wu For (i=1;i<=n;i++) - { About scanf ("%s", str); $ if (str[0]== ' L ') q+=a[i]; - } - printf ("%d\n", q); - */ ADfs0,0,0,1,0); +Dfs (0,0,0, n/2+1,0); the - if(ans==-0x37373737) $printf"Impossible"); the Else the { the //printf ("%d\n", Ans); the for(i=n;i>n/2; i--) - { inans[i]=a2%3; thea2=a2/3; the } About for(i=n/2; i>=1; i--) the { theans[i]=a1%3; thea1=a1/3; + } - for(i=1; i<=n;i++) the if(ans[i]==0)Bayiprintf"lm\n"); the Else the if(ans[i]==1) -printf"mw\n"); - Else the if(ans[i]==2) theprintf"lw\n"); the } the}
Codeforces Round #325 (Div. 1) D. Lizard era:beginning