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
[[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的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile

    题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...

  2. UVA-12166 Equilibrium Mobile(二叉树)

    题目大意:改变二叉树上的权值,使树平衡,问最少该几个值. 题目分析:不会做,查的题解.有条奇妙的性质:如果将第d层权值为w的节点为基准做改动,则整棵树的总重量为w<<d,即w*2^d.仔细 ...

  3. UVA 12166 Equilibrium Mobile

    题意: 给出数个天平,每个天平的结构都类似于二叉树,只有左右重量都相等时才平衡,求每个天平最少改多少个秤砣,也就是叶子结点可以使得整个天平平衡.天平的深度不超过16. 分析: 要使得改动的数量最少,那 ...

  4. UVA 12166 Equilibrium Mobile(贪心,反演)

    直接贪心.先想想最后平衡的时候,如果知道了总重量,那么每一个结点的重量其实也就确定了. 每个结点在左在右其实都不影响,只和层数有关.现在反过来,如果不修改某个结点,那么就可以计算出总质量,取总质量出现 ...

  5. 【习题 6-6 UVA - 12166 】Equilibrium Mobile

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举一个秤砣的重量不变. 某一个秤砣的重量不变之后. 所有秤砣的重量就固定了. 因为它的兄弟节点的重量要和它一样. 则父亲节点的重量 ...

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

    题意:问使天平平衡需要改动的最少的叶子结点重量的个数. 分析:天平达到平衡总会有个重量,这个重量可以由某个叶子结点的重量和深度直接决定. 如下例子: 假设根结点深度为0,结点6深度为1,若以该结点为基 ...

  7. uva 839 not so mobile——yhx

    Not so Mobile  Before being an ubiquous communications gadget, a mobile was just a structure made of ...

  8. G - Not so Mobile

    G - Not so Mobile Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu     Des ...

  9. UVa839 Not so Mobile

      我的解法: 建树,递归判断 #include<cstdio> #include<cstring> #include<iostream> #include< ...

  10. uva-699 Not so Mobile (杠杆,巧妙递归)

      Not so Mobile  Before being an ubiquous communications gadget, a mobile was just a structure made ...

随机推荐

  1. elasticsearch6.7 05. Document APIs(10)Reindex API

    9.REINDEX API Reindex要求为源索引中的所有文档启用_source. reindex 不会配置目标索引,不会复制源索引的设置.你需要在reindex之前先指定mapping,分片数量 ...

  2. 安装Java语言的jdk,配置java环境变量

    一.windows 安装jdk win7 下载jdk: 地址   https://www.oracle.com/technetwork/java/javase/downloads/index.html ...

  3. Compiler showing 'pi' symbol on error

    Question: I was testing some code on Coliru, and I got a strange output. I went down the code and co ...

  4. Javascript 自动执行函数(立即调用函数)

    开头:各种原因总结一下javascript中的自动执行函数(立即调用函数)的一些方法,正文如下 在Javascript中,任何function在执行的时候都会创建一个执行上下文,因为function声 ...

  5. 交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别

    交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别 自己之前一直没搞清楚这两个交叉编译器到底有什么问题,特意google一番,总结如下,希望能帮到道上和 ...

  6. 【转】巧用DOS tree命令+批处理 实现 指定文件 批量复制!

    转自:http://www.cnblogs.com/looky/archive/2010/01/24/1655292.html 今天一朋友叫我帮忙解决指定文件批量复制的问题,于是找了一大堆批处理命令, ...

  7. Kotlin入门(11)江湖绝技之特殊函数

    上一篇文章介绍了Kotlin对函数的输入参数所做的增强之处,其实函数这块Kotlin还有好些重大改进,集中体现在几类特殊函数,比如泛型函数.内联函数.扩展函数.尾递归函数.高阶函数等等,因此本篇文章就 ...

  8. 使用spark DStream的foreachRDD时要注意哪些坑?

    答案: 两个坑, 性能坑和线程坑 DStream是抽象类,它把连续的数据流拆成很多的小RDD数据块, 这叫做“微批次”, spark的流式处理, 都是“微批次处理”. DStream内部实现上有批次处 ...

  9. python 遇到的小坑

    由于前端资源紧缺,我的后端系统迟迟等不来它的前端,没办法只好自己来写了.从html,js入门学起,然后照着vue.js的官方教程写了几个实例,从github上clone了一个不错的vue.js模版,填 ...

  10. Excel实用录入技巧

    一.文本录入技巧 输入开头为0的序号 当直接输入单元格中的数字第一个为0时系统会默认去掉 只需要经单元格格式改为文本或者在单元格输入前使用英文状态下的单引号(‘) 例如:'0001 >>& ...