首页 技术 正文
技术 2022年11月21日
0 收藏 457 点赞 3,402 浏览 1441 个字

题意:问使天平平衡需要改动的最少的叶子结点重量的个数。

分析:天平达到平衡总会有个重量,这个重量可以由某个叶子结点的重量和深度直接决定。

如下例子:

UVA – 12166 Equilibrium Mobile (修改天平)(dfs字符串表示的二叉树)

假设根结点深度为0,结点6深度为1,若以该结点为基准(该结点值不变),天平要平衡,总重量是12(6 << 1),而若以结点3为基准,天平要平衡,总重量也是12(3 << 2)。

由此可得,只需要算出以每个结点为基准的总重量,若该重量下对应的叶子结点最多,则使天平在此重量下平衡改变的叶子结点数最少。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char s[MAXN];
map<ll, int> mp;
int cnt;//叶子结点总数
void dfs(int st, int et, int deep){
if(s[st] == '['){
int tmp = ;
for(int i = st + ; i <= et; ++i){
if(s[i] == '[') ++tmp;
if(s[i] == ']') --tmp;
if(!tmp && s[i] == ','){
dfs(st + , i - , deep + );
dfs(i + , et - , deep + );
}
} }
else{
++cnt;
ll sum = ;
for(int i = st; i <= et; ++i){
sum = sum * + s[i] - '';
}
++mp[sum << deep];
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
mp.clear();
cnt = ;
scanf("%s", s);
int len = strlen(s);
dfs(, len - , );
int ans = ;
for(map<long long, int>::iterator it = mp.begin(); it != mp.end(); ++it){
ans = Max(ans, (*it).second);
}
printf("%d\n", cnt - ans);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,982
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,499
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,343
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,126
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,760
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,796