Codeforces 558C. Amr and Chemistry(暴搞)
来源:互联网 发布:海淘比价软件 编辑:程序博客网 时间:2024/05/21 18:46
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.
Amr has n different types of chemicals. Each chemical i has an initial volume of ai liters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.
To do this, Amr can do two different kind of operations.
- Choose some chemical i and double its current volume so the new volume will be 2ai
- Choose some chemical i and divide its volume by two (integer division) so the new volume will be
Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?
The first line contains one number n (1 ≤ n ≤ 105), the number of chemicals.
The second line contains n space separated integers ai (1 ≤ ai ≤ 105), representing the initial volume of the i-th chemical in liters.
Output one integer the minimum number of operations required to make all the chemicals volumes equal.
34 8 2
2
33 5 6
5
solution:
强行枚举它所能到达的0~maxn的步数,其中有些技巧,看代码即可
#include<cstdio>#include<queue>using namespace std;#define mp make_pairconst int maxn = 1e5 + 20;int vis[maxn], step[maxn], ans[maxn];int main(){int n, x, y;scanf("%d", &n);for (int i = 1; i <= n; i++){scanf("%d", &x);queue< pair<int, int> >que;que.push(mp(x, 0));while (!que.empty()){x = que.front().first;y = que.front().second;que.pop();if (x >= maxn||vis[x]==i)continue;vis[x] = i;ans[x]++;step[x] += y;que.push(mp(2 * x, y + 1));que.push(mp(x / 2, y + 1));}}int tmp = 1e9;for (int i = 0; i < maxn; i++)if (ans[i] == n&&tmp > step[i])tmp = step[i];printf("%d\n", tmp);return 0;}
- 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
- 【23.39%】【codeforces 558C】Amr and Chemistry
- 558C Amr and Chemistry
- Codeforces 558C Amr and Chemistry 位操作
- Codeforces 558C Amr and Chemistry 全都变相等
- Codeforces 558C Amr and Chemistry(dp)
- Codeforces 558C Amr and Chemistry(数论+位运算)
- codeforces 558C Amr and Chemistry(位操作)
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
- CF 558C Amr and Chemistry
- codeforces #312C 558C C. Amr and Chemistry(位运算)
- Codeforces 579A. Raising Bacteria(位运算)
- poj 2240 Arbitrage
- 二维码扫描
- 【bzoj2648】【sjy摆棋子】【kd树】
- 深度学习新算法,完成字里行间的情绪识别
- Codeforces 558C. Amr and Chemistry(暴搞)
- Android自定义View 之 View的测量
- 我和我的同伴是如何用具体的代码
- Linux 系统裁剪笔记1
- SwipeRefreshLayout-下拉刷新控件
- android.view.InflateException: Binary XML file line #4: Error inflating class <unknown>
- 偶然间发现---js大脑图beta01版正式发布
- Windows10 运行 VC6.0
- vs查看预编译后的代码