【习题 6-6 UVA

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

枚举一个秤砣的重量不变。 某一个秤砣的重量不变之后。 所有秤砣的重量就固定了。 因为它的兄弟节点的重量要和它一样。 则父亲节点的重量就是这个节点的两倍了。 以此类推可以得到所有节点的重量的值。 第i层应该的重量都是相同的。 用一个map[height][x]统计某层里面有多少个数字x即可。 这样只要循环层数次就好。 复杂度够过了。

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
*/
#include <bits/stdc++.h>
using namespace std;

string s;
int n,total;
map <long long,int> mmap[20];
map <int,vector <int> > mmap2;

void dfs(int l,int r,int dep){
	if (s[l]=='['){
		int now = 0,i = l+1;

		do{
			switch (s[i]){
			 	case '[':{
			 		now++;
			 	 	break;
			 	}
			 	case ']':{
			 		now--;
			 	 	break;
			 	}
			}
			i++;
		}while (now!=0);
//		now==0;
		while (s[i]!=',') i++;
//		s[i]==','
		dfs(l+1,i-1,dep+1),dfs(i+1,r-1,dep+1);			
	}else{
	 	int x = 0;
	 	for (int i = l;i <= r;i++) x = x*10 + s[i]-'0';
	 	mmap[dep][x]++;
	 	mmap2[x].push_back(dep);
	 	total++;
	}
}	

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("F:\c++source\rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
	int T;
	cin >> T;
	while (T--){
		for(int i = 0;i<=18;i++) mmap[i].clear();
		mmap2.clear();
		total = 0;          
		cin >> s;
		n = s.size();
		int ans = -1;
		if (s[0]!='['){
			ans = 0;
		}else{
			dfs(0,n-1,0);
			for (auto temp:mmap2){
				long long x = temp.first;
				auto v = temp.second;
				for (auto dep:v){
					int tempans = 0;
				 	int now = dep;
					long long xx = x;
					while (xx<=(long long) 1e10 && now >= 0){
						tempans += mmap[now][xx];
						now--;
						xx*=2;					 	
					}
					xx = x;
					now = dep;
					while (xx%2==0 && now <= 18){
						xx/=2;
						now++;					 	
						tempans += mmap[now][xx];
					}
					if (ans==-1){
					 	ans = total - tempans;
					}else{
					 	ans = min(ans,total-tempans);
					}
				}
		 	}
		}
		cout << ans << endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7878423.html