The main topic: Give a binary tree, the whole tree is a balance, each node has a weight or a balance, for any one balance, the rope is in the midpoint, each weight has weights, the minimum number of changes to modify the weight of the balance of the entire balance.
The crux of the matter is a conclusion: if the balance is final, then all nodes at the same depth, regardless of their ancestor nodes, should have the same weight. And the weight of the upper layer should be twice times the next layer. The proof is actually obvious.
Then only need to divide all the nodes, and then take the most nodes of the block, the rest of the nodes are to be modified, is the answer.
The rule of chunking is derived according to the conclusion: assume that the node U is in layer I, V in the Layer J and I<=j, then you and V are equivalent to u.weight*2^ (j-i) =v.weight in the same piece.
#include <stdio.h> #include <stdlib.h> #include <string.h>typedef long long LL; LL A[200010];char e[3100000];int Cmpa (const ll*i,const LL*J); int main (void) {int i,v,pi,qi,top,sump,lo,level,max;scanf ("%d", &pi); while (getchar () = = ") {;} for (qi=0;qi<pi;qi++) {gets (e+1); Lo=strlen (e+1); Level=top=0;i=1;while (I<=lo) {if (e[i]>= ' 0 ') && (E [i]<= ' 9 ')) {sump=0;while ((e[i]>= ' 0 ') && (e[i]<= ' 9 ')) {sump=sump*10+e[i]-' 0 '; i++;} top++;a[top]= (LL) sump<< (LEVEL-1);} else if (e[i]== ' [') {level++;i++;} else if (e[i]== '] ') {level--;i++;} else{i++;}} if (top==0) {printf ("0\n");} Else{qsort (A+1,top,sizeof (a[1]), Cmpa), Max=0;v=1;for (i=2;i<=top;i++) {if (a[i]==a[i-1]) {v++;} else{max= (V>max)? V:max;v=1;}} max= (V>max) v:max;printf ("%d\n", Top-max);}} return 0;} int Cmpa (const ll*i,const ll*j) {if (*i-*j>0) {return 1;} else if (*i-*j<0) {return-1;} Else{return 0;}}
UVA 12166-equilibrium Mobile (derivation conclusion)