Eighty seventh title UVa12166 Equilibrium Mobile

A mobile is a type of kinetic sculpture constructed to
take advantage of the principle of equilibrium. It consists of a number of rods, from which weighted objects
or further rods hang. The objects hanging from the
rods balance each other, so that the rods remain more
or less horizontal. Each rod hangs from only one string,
which gives it freedom to rotate about the string.
We consider mobiles where each rod is attached to
its string exactly in the middle, as in the gure underneath. You are given such a conguration, but the
weights on the ends are chosen incorrectly, so that the
mobile is not in equilibrium. Since that’s not aesthetically pleasing, you decide to change some of the
weights.
What is the minimum number of weights that you
must change in order to bring the mobile to equilibrium? You may substitute any weight by any (possibly non-integer) weight. For the mobile shown in
the gure, equilibrium can be reached by changing the middle weight from 7 to 3, so only 1 weight needs
to changed.
Input
On the rst line one positive number: the number of testcases, at
most 100. After that per testcase:
• One line with the structure of the mobile, which is a recursively
dened expression of the form:
< expr > ::= < weight > | “[” < expr > “,” < expr >
“]”
with < weight > a positive integer smaller than 109
indicating
a weight and ‘[< expr >,< expr >]’ indicating a rod with the two expressions at the ends of
the rod. The total number of rods in the chain from a weight to the top of the mobile will be at
most 16.
Output
Per testcase:
• One line with the minimum number of weights that have to be changed.
Sample Input
3
[[3,7],6]
40
[[2,3],[4,5]]
Sample Output
1
0
3

[Analysis]
consider the equilibrium state, the quality of each weight is determined (weight about two leaf nodes must be equal, otherwise it is not true), then the idea is to take all the weight of n by n minus this number number, is the answer

// 位运算的优先级 比较低   比 加减还低 
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream> 
#include<map>
#include<cstdlib>

using namespace std;
string s;
map<long long ,int> Q;
int sum;

void DFS(int depth,int s1,int e1) {
	if(s[s1] == '[') {
		int p = 0;
		for(int i=s1+1; i!=e1; i++) {
			if(s[i] == '[') p++;
			if(s[i] == ']') p--;
			if(s[i] == ',' && p == 0) {
				DFS(depth + 1,s1 + 1,i - 1);
				DFS(depth + 1,i + 1,e1 - 1);
			}
		}
	}
	else {
		long long w = 0;
		for(int i=s1; i<=e1; i++) w=w*10+s[i]-'0';
		sum ++;
		if(Q.count(w << depth)) {       //根据深度求得最终整个天平的重量 
			Q[w << depth]++;       //映射的值会加一 
		}
		else Q[w << depth] = 1;
	}
}

int main(int argc,char* argv[]) {
	int T; scanf("%d",&T);
	while(T--) {
		cin >> s;
		sum = 0;
		Q.clear();
		DFS(0,0,s.length() - 1);
		int Maxsum = 0;
		for(map<long long,int>:: iterator it = Q.begin(); it != Q.end(); it++)
			Maxsum = max(Maxsum,it->second);
		int Ans = sum - Maxsum;
		printf("%d\n",Ans);
	}
	
	
	return 0;
}

Published 760 original articles · won praise 36 · views 170 000 +

Guess you like

Origin blog.csdn.net/qq_35776409/article/details/104258838