這題思維難度很大,關(guān)鍵是總結(jié)這個(gè)性質(zhì)。
1.天平性質(zhì):某個(gè)秤砣重量為w,高度為h,如果要讓這個(gè)天平平衡并且以這個(gè)秤砣為基準(zhǔn),那么整個(gè)天平的總重量為w*(2^h)
2.利用這個(gè)性質(zhì):題目要求秤砣數(shù)量改變最少,就是說盡量多的不改變秤砣重量,把總質(zhì)量作為主鍵,統(tǒng)計(jì)總質(zhì)量相同的秤砣個(gè)數(shù),
最后計(jì)算出數(shù)量最多的,就是不用改變質(zhì)量的最大秤砣數(shù)量,用所有秤砣數(shù)減去不用改變質(zhì)量的最大秤砣數(shù)量就是答案。
3.當(dāng)然,用這個(gè)性質(zhì),會(huì)讓某些秤砣的質(zhì)量變?yōu)樾?shù)。
4.注意,總重量可能會(huì)變成long long類型。
AC代碼:
#include<cstdio>#include<cstring>#include<map>using namespace std;#define max(x,y) (x) > (y) ? (x) : (y)typedef long long LL;const int maxn = 1e6 + 5;char str[maxn];map<LL, int>ha;int node; //numbers of nodevoid dfs(int l, int r, int h){ if(str[l] == '[') { int p = 0; for(int i = l + 1; i < r ; ++i){ if(str[i] == '[') ++p; else if(str[i] == ']') --p; else if(str[i] == ',' && p == 0) { dfs(l + 1, i - 1, h + 1); //Left dfs(i + 1, r - 1, h + 1); //Right } } } else { ++node; int num = 0; while(l <= r) num = num * 10 + str[l++] - '0'; ha[(LL)num << h]++; }}int main(){ int T; scanf("%d", &T); while(T--) { node = 0; scanf("%s", str); int n = strlen(str); dfs(0, n-1, 0); int ans = 0; for(map<LL, int>::iterator c = ha.begin(); c != ha.end(); ++c) { ans = max(ans, c->second); } PRintf("%d/n",node - ans); ha.clear(); } return 0;}如有不當(dāng)之處歡迎指出!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注