1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 5 /**6 idea: Enumerate right and left subtrees at each root node7 8 Learning:9 (1) enumeration of subsets of methods such as enumerations of S = 100101Ten for (int l = (s-1) &s, l > 0; l = (l-1) & s) { One int r = s ^ l; A (2) Thinking: How to represent the left distance to the right. The left arm/right arm length of the point is determined by a subset:-sumw[r]/(Sumw[l] + sumw[r]) + NODE[L][I].L - Sumw[i]: Represents the weight of subset I - NODE[I].L: Indicates the left arm length of subset I the Left arm + right sub-balance's left arm--the lower arm--who is smaller - right arm + starboard balance with left dial hand balance on arm-left arm who 's bigger - */ - + - structnode{ + DoubleL,r;//The leftmost extension point of the sub-balance that holds the point, the right-most extension point A Node () {} atNode (DoublellDoubleRR): L (LL), R (RR) {} - }; - intN; - Doubler,w[ A],ans,sumw[1<<6]; - intvis[1<<6]; -Vector<node> node[1<<6]; in intBitcount (intx) { - if(!x)return 0; to returnBitcount (x>>1) + (x&1); + } - voidDfsints) { the if(Vis[s])return ; *Vis[s] =true; $ if(Bitcount (s) = =1){Panax NotoginsengNode[s].push_back (node{0,0}); - return ; the } + for(intL = (s1) &s; L >0; L = (l1) &s) { A intR = S ^l; theDFS (L); DFS (R);//all scenarios for a two subset of a root node. Node[l].size () left scheme number node[r].size () to the right of the scheme number + for(inti =0; I < node[l].size (); i + +){ - for(intj =0; J < Node[r].size (); J + +){ $ Doublell = min (-sumw[r]/(Sumw[l] + sumw[r]) + NODE[L][I].L, Sumw[l]/(Sumw[l] + sumw[r]) +node[r][j].l); $ Doublerr = max (Sumw[l]/(Sumw[l] + sumw[r]) + NODE[R][J].R,-sumw[r]/(Sumw[l] + sumw[r]) +NODE[L][I].R); - Node[s].push_back (Node (ll, RR)); - } the } - }Wuyi } the - voidsolve () { Wumemset (Vis,0,sizeof(Vis)); -memset (node,0,sizeof(node)); AboutAns =-1; $ ints = (1<<n)-1; - Dfs (s); - for(inti =0; I < node[s].size (); i + +){ - if(Node[s][i].r-node[s][i].l < R && NODE[S][I].R-NODE[S][I].L >ans) { AAns = NODE[S][I].R-NODE[S][I].L; + } the } - if(ans = =-1) printf ("-1\n"); $ Elseprintf"%.10lf\n", ans); the } the the intMain () { the intT; -scanf"%d",&T); in while(t--){ thescanf"%lf%d",&r,&n); the for(inti =0; I < n; i + +) scanf ("%LF",&w[i]); About for(inti =0; I < (1<< N); i + +){ theSumw[i] =0; the for(intj =0; J < N; J + +){ the if(I & (1<< j)) Sumw[i] + =W[j]; + } - } the solve ();Bayi } the return 0; the}
UVA 1354Mobile Computing (balance puzzle, enumeration subset, recursion, good title *)