博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【23.39%】【codeforces 558C】Amr and Chemistry
阅读量:4624 次
发布时间:2019-06-09

本文共 3705 字,大约阅读时间需要 12 分钟。

time limit per test1 second

memory limit per test256 megabytes
inputstandard input
outputstandard output
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?

Input

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

Output one integer the minimum number of operations required to make all the chemicals volumes equal.

Examples

input
3
4 8 2
output
2
input
3
3 5 6
output
5
Note
In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.

In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1.

【题目链接】:

【题解】

先搞出来每个数字能变成什么数字(最大到1e5就好,不放心就加大一点);
数字能够乘2也能除2;
但不是猛地一直乘或一直除;
如果数字是
3
则3/2=1
但是这个时候
2 4 8 16都能达到了;
即1*2,1*2*2,1*2*2*2….
则除的时候要多判断一下这个数是不是奇数;
如果是奇数它除完之后还能一直乘.
因为最后要求所有的数字都变成同一个数字;
则我们在处理每一个数字到达的数字的时候,可以直接累加这个数字要经过多少步到达;
最后O(N)枚举求最小值就好;(n个数字都能变成这个数字的话);
用map会T…
【完整代码】

#include 
using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)#define mp make_pair#define pb push_back#define fi first#define se second#define rei(x) scanf("%d",&x)#define rel(x) scanf("%I64d",&x)#define pri(x) printf("%d",x)#define prl(x) printf("%I64d",x)typedef pair
pii;typedef pair
pll;const int MAXN = 1e5+10;const int dx[9] = {
0,1,-1,0,0,-1,-1,1,1};const int dy[9] = {
0,0,0,-1,1,-1,1,-1,1};const double pi = acos(-1.0);int n;int a[MAXN];int dic[MAXN],dic3[MAXN];int main(){ //freopen("F:\\rush.txt","r",stdin); rei(n); rep1(i,1,n) rei(a[i]); rep1(i,1,n) { int step = 0; int now = a[i]; dic[now]++; dic3[now]+=step; now<<=1; while (now <= 1e5) { dic[now]++; step++; dic3[now]+=step; now<<=1; } now = a[i]; step = 0; while (now >0) { if (now&1) { int tnow = now>>1; if (tnow<=0) break; int tstep = step+1; tnow <<=1; tstep++; while (tnow <= 1e5) { dic[tnow]++; dic3[tnow]+=tstep; tstep++; tnow<<=1; } } now>>=1; if (now <=0) break; step++; dic[now]++; dic3[now]+=step; } } int ans = 21e8; rep1(i,0,1e5) if (dic[i]==n) ans = min(ans,dic3[i]); pri(ans); return 0;}

转载于:https://www.cnblogs.com/AWCXV/p/7626844.html

你可能感兴趣的文章
poj2114 Boatherds
查看>>
maven学习(上)- 基本入门
查看>>
20165231 实验一 Java开发环境的熟悉
查看>>
移动商城第八篇【添加商品之基本属性和大字段数据(FCK文本编辑器)】
查看>>
Solr学习总结(六)SolrNet的高级用法(复杂查询,分页,高亮,Facet查询)
查看>>
对象序列化和反序列化
查看>>
6.10
查看>>
lcd_1602
查看>>
三年工作总结
查看>>
循环不变式证明算法的正确性
查看>>
实现算法2.15、2.16的程序(一个数组只生成一个静态链表)
查看>>
java常用设计模式
查看>>
Web开发中的网络知识(传输层)
查看>>
Shell脚本8种字符串截取方法总结
查看>>
串模式匹配——KMP算法
查看>>
贪心讲解链接
查看>>
初识MyBatis
查看>>
在线HTML编辑器Kindeditor-4.1.10简易示例
查看>>
深入浅出Windows命令——telnet
查看>>
jQuery事件与事件对象
查看>>