第八十七题 UVa12166 Equilibrium Mobile

2023-11-07

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 Output
1
0
3

【分析】
考虑一下平衡状态,每个砝码的质量都是确定的,(左右两个叶子结点的重量必须相等,否则不成立),那么思路就是n个砝码取众数 用n减去这个数,就是答案

// 位运算的优先级 比较低   比 加减还低 
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream> 
#include<map>
#include<cstdlib>

using namespace std;
string s;
map<long long ,int> Q;
int sum;

void DFS(int depth,int s1,int e1) {
	if(s[s1] == '[') {
		int p = 0;
		for(int i=s1+1; i!=e1; i++) {
			if(s[i] == '[') p++;
			if(s[i] == ']') p--;
			if(s[i] == ',' && p == 0) {
				DFS(depth + 1,s1 + 1,i - 1);
				DFS(depth + 1,i + 1,e1 - 1);
			}
		}
	}
	else {
		long long w = 0;
		for(int i=s1; i<=e1; i++) w=w*10+s[i]-'0';
		sum ++;
		if(Q.count(w << depth)) {       //根据深度求得最终整个天平的重量 
			Q[w << depth]++;       //映射的值会加一 
		}
		else Q[w << depth] = 1;
	}
}

int main(int argc,char* argv[]) {
	int T; scanf("%d",&T);
	while(T--) {
		cin >> s;
		sum = 0;
		Q.clear();
		DFS(0,0,s.length() - 1);
		int Maxsum = 0;
		for(map<long long,int>:: iterator it = Q.begin(); it != Q.end(); it++)
			Maxsum = max(Maxsum,it->second);
		int Ans = sum - Maxsum;
		printf("%d\n",Ans);
	}
	
	
	return 0;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

第八十七题 UVa12166 Equilibrium Mobile 的相关文章

  • 第八十七题 UVa12166 Equilibrium Mobile

    A mobile is a type of kinetic sculpture constructed to take advantage of the principle of equilibrium It consists of a n
  • 数论——欧拉函数

    在数论中 对正整数n 欧拉函数是少于或等于n的数中与n互质的数的数目 此函数以其首名研究者欧拉命名 它又称为Euler s totient function 函数 欧拉商数等 例如 8 4 因为1 3 5 7均和8互质 百度百科词条 欧拉函
  • 素数打表,复杂度(Onlogn)和O(n)(对与10^7来说线性快两倍) + 分解质因数

    代码 接口 primeInit 100000 打表的范围 素数存在primeList中 个数为primeCount typedef long long LL int const MAXN 10000100 bool isPrime MAXN
  • 【刷题】华为笔试面试机考 [HJ5] - 进制转换

    题目地址 点击跳转 题目描述 写出一个程序 接受一个十六进制的数 输出该数值的十进制表示 输入描述 输入一个十六进制的数值字符串 注意 一个用例会同时有多组输入数据 请参考帖子https www nowcoder com discuss 2
  • uva10105(数论多项式展开公式)

    题意 多项式 x1 x2 xk n 输入n和k 0
  • 首字母变大写

    小写字母变大写m 0 m 0 32 include
  • LightOJ 1045 Digits of Factorial

    Problem acm hust edu cn vjudge problem visitOriginUrl action id 26765 分析 在base进制下 pow base x 表示最小的 x 1 位数 pow base x 1 表
  • UVa10881题解报告

    题目 L长的棍子上有n个蚂蚁 他们分别向左或右爬 速度为1 求T时间后各蚂蚁的状态 题解 白书给出了一个很巧妙的解法 将蚂蚁看作质点 相撞掉头等于对穿而过 因为掉头所以 他们最后的顺序与输入时在棍子上的顺序相同 所以只要记录下初始状态下蚂蚁
  • hdu 4712 Hamming Distance

    Problem acm hdu edu cn showproblem php pid 4712 Reference 多向 bfs 思路 CSDN markdown 用 LaTeX Meaning 定义两个整数数 a 和 b 的汉明距离为 a
  • hdu 6121 Build a tree

    Problem acm hdu edu cn showproblem php pid 6121 Meaning 一棵 n 个点的完全 k 叉树 结点标号从 0 到 n 1 求以每一棵子树的大小的异或和 Analysis 一层层地统计答案 找
  • 二叉搜索树(树状数组)

    计数函数 程序 int lowbit int k return k k 功能 可视为每个节点的编号函数 加和函数 程序 int sum int x int ret 0 while x gt 0 ret c x x lowbit x retu
  • HDU1007(最近点对问题)

    题意不难理解 就是找到最近的两个点 计算其距离 除以2就是所求的圆的半径 思路很简单 运用分治的思想 先划分区间 分别找到左右区间中的最近点对 再合并区间 找到区间间的最近点对 注意如果用qsort 进行排序可能会超时 include
  • gym 101505 CTU Open Contest 2016 G Orchard Division

    Problem codeforces com gym 101505 attachments vjudge net contest 187874 problem G Meaning 一个 m m 的网格 长 宽下标 0 m 1 里有 n 个点
  • hdu 5792 World is Exploding 2016 Multi-University 5

    Problem acm hdu edu cn showproblem php pid 5792 题意 给一个序列 V 问有多少个由下标组成的四元组 a b c d 满足 a b c d a lt b c lt d Va lt Vb Vc g
  • ACM-子串(字符串处理)

    问题描述 有一些由英文字符组成的大小写敏感的字符串 请写一个程序 找到一个最长的字符串 x 使得 对于已经给出的字符串中的任意一个 y x 或者是 y 的子串 或者 x 中的字符反序之后得到的新字符串是 y 的子串 输入数据 输入 输入的第
  • hduoj 2010

    水仙花数 Problem Description 春天是鲜花的季节 水仙花就是其中最迷人的代表 数学上有个水仙花数 他是这样定义的 水仙花数 是指一个三位数 它的各位数字的立方和等于其本身 比如 153 1 3 5 3 3 3 现在要求输出
  • kuangbin的模板

    直接链接 间接链接
  • UVA 1347 Tour

    描述 Click Here quad 给定平面上n n lt 1000 个点的坐标 按照x递增的顺序给出 各点x坐标不同 且均为整数 你的任务是设计一条路线 从最左边的点出发走到最右边的点再返回 要求除了最左边和最右边之外 每个点恰好经过一
  • 蓝桥杯 填字母游戏(博弈论)

    小明经常玩 LOL 游戏上瘾 一次他想挑战K大师 不料K大师说 我们先来玩个空格填字母的游戏 要是你不能赢我 就再别玩LOL了 K大师在纸上画了一行n个格子 要小明和他交替往其中填入字母 并且 1 轮到某人填的时候 只能在某个空格中填入L或
  • 杭电ACM 1004题

    原题大概意思就是统计输入字符串中 重复的最大个数 import java util Scanner public class Main public static void main String args Scanner sc new S

随机推荐

  • 软件测试——基础理论知识你都不一定看得懂

    目录 前言 软件测试 Software Testing 的定义 软件测试的分类 软件测试的常用种类 测试用例八大设计方法 结语 前言 入软件测试这一行至今已经10年多 承蒙领导们的照顾与重用 同事的支持与信任 我的职业发展算是相对较好 从入
  • Pyppeteer的使用——爬取京东

    1 Pyppeteer优势 不用像Selenium一样配置浏览器环境 可以直接在页面上进行爬取 爬取的不是页面源码而是已经加载完毕的 显示在浏览器上的页面 可以绕过加密系统 Pyppeteer加载的text 是加载完成后的HTML页面 所有
  • ad如何计算电路板的pin数量_一招教你学会使用AD更改PCB板子尺寸!

    原标题 一招教你学会使用AD更改PCB板子尺寸 使用原理图生成PCB后 Altium Designer会自动生成一块黑色区域 还有一个在禁止布线层的方框 还有两段标注板子大小的线 下面说一下如何更改黑色区域的大小 还有如何精确确定板子尺寸
  • DES密码算法实现(C语言)

    算法介绍 DES算法为密码体制中的对称密码体制 又被称为美国数据加密标准 是1972年美国IBM公司研制的对称密码体制加密算法 明文按64位进行分组 密钥长64位 密钥事实上是56位参与DES运算 第8 16 24 32 40 48 56
  • 【腾讯云TDSQL-C Serverless 产品测评】一文带你了解TDSQL-C Serverless版

    文章目录 前言 腾讯云TDSQL C for MySQL Serverless版介绍 准备工作 1 购买TDSQL C for MySQL Serverless版实例 2 开启数据库外网访问 3 安装测试工具 4 准备测试数据 Server
  • 记录android遇到的SecurityException

    记录android遇到的SecurityException 一 java lang SecurityException getUniqueDeviceId The user 10283 does not meet the requireme
  • List和ArrayList

    List和ArrayList区别 List是一个接口 而ArrayList是List接口的一个实现类 ArrayList类继承并实现了List接口 因此 List接口不能创建实例对象 但是可以为List接口创建一个指向自己的对象引用 而Ar
  • 微信公众号小程序开通方法_微信小程序发布审核大概要多久

    1 如果自己有通过微信公众平台注册认证好微信公众号 那么只需要登录微信公众号账号后在页面左侧找到小程序管理 注册认证小程序账号 注意 公众号账号和小程序账号是独立的两个账号 2 如果自己没有注册认证微信公众号 就可以先到微信公众平台注册个微
  • ubuntu 18.04下virtualbox安装windows虚拟机+增强功能+secureCRT

    先强调一下 我是在Ubuntu里安装windows虚拟机 如果要看如何安装linux虚拟机的话 那么你走错地方了 我一直使用Linux系统做开发的 选择Ubuntu是因为多数常用软件对Ubuntu支持的不错 能少折腾就少折腾 程序员的时间不
  • Qt5.12.3移植rk3399pro笔记

    Qt5 12 3移植到rk3399pro笔记 环境 主机 Ubuntu16 04 目标机 rk3399pro板 x11平台 交叉编译toolchain linux aarch64 gnu 问题描述 我的目标机是debian系统 带lxde桌
  • 【大数据】HiveQL的数据操作

    HiveQL的数据操作 因为 Hive 没有行级别的数据插入 数据更新和删除操作 那么往表中装载数据的唯一途径就是使用一种 大量 的数据装载操作 或者通过其他方式仅仅将文件写入到正确的目录下 1 向管理表中装载数据 LOAD DATA LO
  • SpringFactoriesLoader ServiceLoader区别

    内容简介 IoC 并不仅限于解决模块内类与类之间的依赖耦合问题 其同样适用于模块与模块之间 OSGi 一直致力于这方面的工作 但其实 Java 和 Spring 都提供了对 IoC 的支持 Java 本身提供了一种很简便的方式来支持 IoC
  • 报告

    来源 Prophet 2019年 战略数字化转型的重要性已经不止于IT领域 而影响着全公司的竞争力 企业的相关预算直线攀升 利益相关方所关注的颠覆性技术数量急剧增加 数字化项目开始由首席高管主导 并由相互协作的跨职能团队管理 数字化是整个企
  • 爬虫项目五:最详细的京东商品、评价爬虫、词云展示

    文章目录 前言 一 京东商品信息爬虫 1 分析URL 2 实例化chrome 3 加载完整数据 4 实现翻页 5 解析数据 二 京东商品评价爬虫 1 找到接口 2 分析url 3 解析数据 4 词云 前言 本文内容包含京东商品列表爬虫的详细
  • pycharm启动报错

    1 点击pycharm 报错 2 打开cmd 输入gpedit msc 点击 确定 3 在本地组策略编辑器 选择 Windows设置 安全设置 本地策略 安全选项 用户帐户控制 用于内置管理员帐户的管理员批准模式 4 设置 用户帐户控制 用
  • cuda历史版本和cudnn的下载地址

    cuda历史版本下载地址 https developer nvidia com cuda toolkit archive cudnn下载地址 https developer nvidia com rdp cudnn archive 欢迎大家
  • pthread_mutex_init线程互斥锁的使用

    pthread mutex init 头文件 include
  • Springboot项目中@JsonProperty不生效-如何处理呢?

    转自 Springboot项目中 JsonProperty不生效 如何处理呢 下文笔者讲述SpringBoot中 JsonProperty不生效的相关简介说明 首先笔者将讲述JsonProperty注解的功能简介说明 JsonPropert
  • googlecloud谷歌云的初学体会(1)

    googlecloud谷歌云入门 1 一 纯小白自述 二 云是个什么云 三 装一个软件 资源 服务 四 服务器 爷爷提供服务的电脑 五 PGSQL的安装 六 总结 一 纯小白自述 自己是个小白 仅仅懂得几句sql查询和编程的基础语法 云是啥
  • 第八十七题 UVa12166 Equilibrium Mobile

    A mobile is a type of kinetic sculpture constructed to take advantage of the principle of equilibrium It consists of a n