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 ...
随机推荐
- [NOI 2017]整数
Description 题库链接 P 博士将他的计算任务抽象为对一个整数的操作. 具体来说,有一个整数 \(x\) ,一开始为 \(0\) . 接下来有 \(n\) 个操作,每个操作都是以下两种类型中 ...
- Ansible系列(五):各种变量定义方式和变量引用
Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.1 ansible facts facts组件是用来收集被管理节点信息的 ...
- SpringBoot入门之基于Druid配置Mybatis多数据源
上一篇了解了Druid进行配置连接池的监控和慢sql处理,这篇了解下使用基于基于Druid配置Mybatis多数据源.SpringBoot默认配置数据库连接信息时只需设置url等属性信息就可以了,Sp ...
- spring boot(一):Hello World
前言 作为程序员,不管是.net程序员还是java程序员其实从骨子里都不太喜欢各种配置文件的,记得刚开始学java SSH时动不动就装B,来看看我的配置多不多,又是从.net开始写java的程序员提起 ...
- Shell 示例:将指定的文件内容转换为大写
程序代码如下: #!/bin/bash # 将一个指定的输入文件内容转换为大写 E_FILE_ACCESS=70 E_WRONG_ARGS=71 if [ ! -r "$1" ] ...
- ajax读取txt文本时乱码的解决方案
前言:第一次学习使用 ajax 就是用来读取文本 先给出现乱码的代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...
- Ocelot中文文档-Not Supported
Not Supported Ocelot不支持以下几种情况 块级编码(Chunked Encoding ) - Ocelot始终会获取消息体的大小并返回内容长度(Content-Length).这种情 ...
- 算法第四版-文字版-下载地址-Robert Sedgewick
下载地址:https://download.csdn.net/download/moshenglv/10777447 算法第四版,文字版,可复制,方便copy代码 目录: 第1章 基 础 ...... ...
- HDU4287
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- JQuery基本知识汇总;JQuery常用方法;浅入了解JQuery
一.jQuery对象与JavaScript对象 ①JavaScript入口函数比jQuery入口函数执行的晚一些: JQuery的入口函数会等页面加载完成才执行,但是不会等待图片的加载: JavaSc ...