UVA12166-Equilibrium Mobile
Problem UVA12166-Equilibrium Mobile
Accept:529 Submit:4330
Time Limit: 3000 mSec
Problem Description
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,7],6]
40
[[2,3],[4,5]]
Sample Ouput
1
0
3
题解:这个题还是比较需要思维的,一开始考虑DP,想状态想得云里雾里,看了题解才发现做法如此机智。
对于一个已经平衡得天平,可以把砝码拆开,构造出来一个满二叉树,思路就是从这里来的,由于要求修改最少的砝码,所以必定会以一个砝码作为参照,或者说该砝码质量不变,这样以来,根据刚才的思路,整个天平的质量就出来了,对于每一个砝码都进行这样的操作,求出来n个总质量,取其中的众数,再用n减去该数就得到了最少要修改几个。
这个题读入的技巧很有必要学习一下。对于这种只在叶子节点有权值的二叉树来说,这个建树方式非常便捷。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <map>
using namespace std;
typedef long long LL; int cnt,sum;
char exp[];
map<LL,int> ID; void dfs(int l,int r,int depth){
if(exp[l] == '['){
int p = ;
for(int i = l+;i <= r;i++){
if(exp[i] == '[') p++;
else if(exp[i] == ']') p--;
if(p== && exp[i]==','){
dfs(l+,i-,depth+);
dfs(i+,r-,depth+);
}
}
}
else{
int weight;
sscanf(exp+l,"%d",&weight);
ID[(LL)weight*(<<depth)]++;
sum++;
}
} int main()
{
//freopen("input.txt","r",stdin);
int iCase;
scanf("%d",&iCase);
while(iCase--){
ID.clear();
cnt = ,sum = ;
scanf("%s",exp);
dfs(,strlen(exp)-,);
map<LL,int>::iterator iter;
int Max = ;
for(iter = ID.begin();iter != ID.end();iter++){
Max = Max > iter->second ? Max : iter->second;
}
printf("%d\n",sum-Max);
}
return ;
}
UVA12166-Equilibrium Mobile的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile
题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...
- UVA-12166 Equilibrium Mobile(二叉树)
题目大意:改变二叉树上的权值,使树平衡,问最少该几个值. 题目分析:不会做,查的题解.有条奇妙的性质:如果将第d层权值为w的节点为基准做改动,则整棵树的总重量为w<<d,即w*2^d.仔细 ...
- UVA 12166 Equilibrium Mobile
题意: 给出数个天平,每个天平的结构都类似于二叉树,只有左右重量都相等时才平衡,求每个天平最少改多少个秤砣,也就是叶子结点可以使得整个天平平衡.天平的深度不超过16. 分析: 要使得改动的数量最少,那 ...
- UVA 12166 Equilibrium Mobile(贪心,反演)
直接贪心.先想想最后平衡的时候,如果知道了总重量,那么每一个结点的重量其实也就确定了. 每个结点在左在右其实都不影响,只和层数有关.现在反过来,如果不修改某个结点,那么就可以计算出总质量,取总质量出现 ...
- 【习题 6-6 UVA - 12166 】Equilibrium Mobile
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举一个秤砣的重量不变. 某一个秤砣的重量不变之后. 所有秤砣的重量就固定了. 因为它的兄弟节点的重量要和它一样. 则父亲节点的重量 ...
- UVA - 12166 Equilibrium Mobile (修改天平)(dfs字符串表示的二叉树)
题意:问使天平平衡需要改动的最少的叶子结点重量的个数. 分析:天平达到平衡总会有个重量,这个重量可以由某个叶子结点的重量和深度直接决定. 如下例子: 假设根结点深度为0,结点6深度为1,若以该结点为基 ...
- uva 839 not so mobile——yhx
Not so Mobile Before being an ubiquous communications gadget, a mobile was just a structure made of ...
- G - Not so Mobile
G - Not so Mobile Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Des ...
- UVa839 Not so Mobile
我的解法: 建树,递归判断 #include<cstdio> #include<cstring> #include<iostream> #include< ...
- uva-699 Not so Mobile (杠杆,巧妙递归)
Not so Mobile Before being an ubiquous communications gadget, a mobile was just a structure made ...
随机推荐
- 【学习笔记】深入理解HTTP协议
参考:关于HTTP协议,一篇就够了,感谢作者认真细致的总结,本文在理解的基础上修改了内容,加深印象的同时也希望对大家有所帮助 HTTP是什么? HTTP协议是Hyper Text Transfer P ...
- MySQL基准测试(三)--开源工具与实例演示
MySQL基准测试(三)--开源工具与实例演示 针对web应用 ab ab是一个Apache HTTP服务的基准测试工具. http_load http_load是一个针对Web服务器测试工具. JM ...
- 玩转物联网之MQTT
物联网概述 物联网——即Internet-of-Things,其实这个概念由来已久,简单来讲,物联网是物与物.人与物之间的信息传递与控制简称.它和能源.电子信息.医疗.交通.零售.物流.工业制造等行业 ...
- 【Tomcat】Tomcat日志切割
下载并解压缩 cronolog # tar zxvf cronolog-1.6.2.tar.gz 2.进入cronolog安装文件所在目录 # cd cronolog-1.6.2 3.运行安装 # ...
- React 入门学习笔记整理(五)—— state
1.state 1)组件本省也是有状态的,定义在组件内部的state中,state的状态只能由组件自身改变,任何其他组件都不能改变. 当需要改变state时,通过调用setState方法来改变,set ...
- “京东金融”主页效果 RecyclerView联动
先上效果图吧: 第一个想到的实现方式是上面使用horizontalScrollview,下面使用Viewpager,经过尝试之后发现二者API有限,不能达到理想效果.几经折腾,最后上下都使用了自定义的 ...
- MySQL 性能优化--优化数据库结构之优化数据类型
MySQL性能优化--优化数据库结构之优化数据类型 By:授客 QQ:1033553122 优化数字数据(Numeric Data) l 对于唯一ID或其它可用字符串或数字表示的值,选择 ...
- kNN处理iris数据集-使用交叉验证方法确定最优 k 值
基本流程: 1.计算测试实例到所有训练集实例的距离: 2.对所有的距离进行排序,找到k个最近的邻居: 3.对k个近邻对应的结果进行合并,再排序,返回出现次数最多的那个结果. 交叉验证: 对每一个k,使 ...
- 自动化测试基础篇--Selenium Python环境搭建
学习selenium python需要的工具: 1.浏览器 2.Python 3.Selenium 4.FireBug(Firefox) 5.chromedriver.IEDriverServer.g ...
- [转]js版的md5()
转自:http://blog.163.com/a_hc/blog/static/1623434200831832441896/ <script language=javascript>/* ...