Test instructions: A balance expression: [b], where a and B are a number or another balance expression. Each balance expression represents a balance of all lengths equal, so if balance is required, both sides are required to be of equal quality. The topic is to give a balance expression, require calculation, in the case of giving expression, if the requirement of horizontal transverse, you need to adjust the minimum number of objects.
Idea: Assuming that there are N objects (weights) on the balance, you need to modify the maximum of N-1, because the worst case is to follow one of the points unchanged, and then other points are based on it, leveling.
So, you can think about it, traverse each weight and base it on it, at least how many will you need to mediate?
Because the quality of a single point in a scale is twice times the mass of a single point in the next layer, it is possible to figure out how much quality is required at the highest level. For example, in the 2nd layer of a weight of 2, then the first layer of quality needs 4;
Comparing each point, according to the number of layers, the calculation can be found if a point is not changed, the highest level requires how much quality, and finally observe the same quality is the most required number of times.
/*uvaoj 12166EmeraldMon 2015*/#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <string> #include <map>using namespace std;string expr;int Needchange;map<long Long, int> hashcount;//build the binary treevoid buildtree (string& S, int& start, int root, int layout) {long Long tmp;if (S[start]! = ' [') {sscanf (&s[start], "%lld", &tmp); if (!hashcount.count (tmp<<layout)) {has Hcount[tmp<<layout] = 1;} else {Hashcount[tmp<<layout] + +;}} else {Buildtree (S, ++start, root*2, layout+1);} for (; s[start]!= ', '; start++) {}start ++;if (S[start]! = ' [') {sscanf (&s[start], "%lld", &tmp); if (!hashcount.c Ount (Tmp<<layout)) {hashcount[tmp<<layout] = 1;} else {hashcount[tmp<<layout] + +;}} else {Buildtree (S, ++start, root*2+1, layout+1);}} void Init () {hashcount.clear ();} void Solve () {needchange = 0;//can ' t be wasted, if it's forgotten, the case ' a ' may wrongif (expr[0]! = ' [') {return;} int pos = 1; Buildtree (expr, POS, 1, 1); Map<long long, int>:: iterator it;int MaxC = -1;int sum = 0;for (it = Hashcount.begin ( ); It! = Hashcount.end (); It + +) {sum + = IT->SECOND;MAXC = MaxC > It->second? Maxc:it->second;} Needchange = SUM-MAXC;} void Print () {printf ("%d\n", Needchange);} void Read () {cin >> expr;} void work () {Init (); Read (); Solve (); Print ();} int main () {int t;cin >> t;while (T-) {work ();} return 0;}
Uva 12166 Equilibrium Mobile