UVA 12166-equilibrium Mobile (Derivation conclusion)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.