Codeforces Round #312 (Div. 2) C. Amr and Chemistry(技巧)
来源:互联网 发布:java电信计费系统 编辑:程序博客网 时间:2024/05/29 08:02
题目:
点击打开链接
题意:
n个瓶子,每个瓶子都有相应的水的数量,瓶子的水可以乘2或者除2,求出最少多少步骤可以使瓶子中的所有水相等。
思路:
将所有瓶子可以到达的毫升数及其步数记录起来,得到可以n个瓶子都能到达的毫升数的最小步数。
AC.
#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;const int maxn = 1e5+5;int step[maxn], cnt[maxn], vis[maxn];int main(){ //freopen("in", "r", stdin); int n; while(~scanf("%d", &n)) { queue<pair<int, int> > q; memset(step, 0, sizeof(step)); memset(vis, 0, sizeof(vis)); memset(cnt, 0, sizeof(cnt)); int a; for(int i = 1; i <= n; ++i) { scanf("%d", &a); q.push(make_pair(a, 0)); while(!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); if(x > maxn - 2) continue; if(vis[x] == i) continue; vis[x] = i; cnt[x]++; step[x] += y; q.push(make_pair(x*2, y+1)); q.push(make_pair(x/2, y+1)); } } int ans = 0x3f3f3f; for(int i = 1; i < maxn; ++i) { if(cnt[i] == n) { ans = min(ans, step[i]); } } printf("%d\n", ans); } return 0;}
0 0
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry(技巧)
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry
- Codeforces Round #312 (Div. 2) C Amr and Chemistry
- Codeforces Round #312 (Div. 2)——C暴力技巧——Amr and Chemistry
- C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry(数论)
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry dp 搜索
- C. Amr and Chemistry(Codeforces Round #312(div2))
- Codeforces Round #312 (Div. 2) Amr and Chemistry(位运算+暴搜)
- Codeforces 558C Amr and Chemistry(dp)
- codeforces 558C Amr and Chemistry(位操作)
- codeforces 558 C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 规律
- Codeforces 558C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 暴力 - -
- codeforces 558C Amr and Chemistry
- Codeforces 558C - Amr and Chemistry (枚举)
- Codeforces 558C Amr and Chemistry
- Maven详解之仓库------本地仓库、远程仓库
- 关于PATHINFO的重写规则解析
- ios後臺播放音樂
- iOS数组count操作(NSUInteger)的安全性
- Microsoft COM+组件接口的方法的重载问题。
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry(技巧)
- java AJAX实现级联下拉框
- Linux虚拟机ftp客户端与Windows ftp服务器建立连接的方法
- 启动tomcat提示内存不足
- Android使用Ant自动编译签名打包详解
- Error pulling origin: error: The following untracked working tree files would be overwritten by
- Jlink FL2440 裸机开发(一) 点亮LED和按键控制
- Unity 接入ShareSDK Android&iOS 微信分享
- javaweb之Tomcat