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.

【题目链接】:http://codeforces.com/contest/558/problem/C

【题解】



先搞出来每个数字能变成什么数字(最大到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 <bits/stdc++.h>
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<int,int> pii;
typedef pair<LL,LL> 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;
}

【23.39%】【codeforces 558C】Amr and Chemistry的更多相关文章

  1. codeforces 558C C. Amr and Chemistry(bfs)

    题目链接: C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input st ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. 【23. 合并K个排序链表】【困难】【优先队列/堆排序】

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6] 输出: 1->1-> ...

  4. codeforces 558/C Amr and Chemistry(数论+位运算)

    题目链接:http://codeforces.com/problemset/problem/558/C 题意:把n个数变成相同所需要走的最小的步数易得到结论,两个奇数不同,一直×2不可能有重叠枚举每个 ...

  5. 【39.66%】【codeforces 740C】Alyona and mex

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【20.23%】【codeforces 740A】Alyona and copybooks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【23.33%】【codeforces 557B】Pasha and Tea

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【30.23%】【codeforces 552C】Vanya and Scales

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. 调用中行接口针对返回报文(xml形式)做相关处理

    最近在对接中行银行接口,在获取返回报文的时候遇到一些问题,现在在这里做个总结 TIP: 在返回报文之前,要对前置机的URL请求,在这期间遇到一个坑,还是通过查看日志才发现问题 在填写转账信息的时候要求 ...

  2. 利用ArcGIS水文分析工具提取河网

    转自原文 利用ArcGIS水文分析工具提取河网(转) DEM包含有多种信息,ArcToolBox提供了利用DEM提取河网的方法,但是操作比较烦琐(帮助可参看Hydrologic analysis sa ...

  3. 智能指针shared_ptr, auto_ptr, scoped_ptr, weak_ptr总结

    看这里: http://blog.csdn.net/lollipop_jin/article/details/8499530 shared_ptr可以多线程同时读,但是涉及到写,需要加锁. share ...

  4. Linux - 用 Konstruct 安装 KDE 3.x

    make-kde3.x Linux - 用 Konstruct 安装 KDE 3.x 找个截图工具 khtml2png (http://khtml2png.sourceforge.net/), 现在都 ...

  5. js33--责任链模式

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. actionMode - 在屏幕中的显示位置设置

    actionMode 默认的显示位置是在屏幕上方的,如果想要移到下方,可以添加如下属性 在AndroidManifest.xml 的activity中,做如下修改 <activity andro ...

  7. 新技能 get —— 如何校验 md5(windows)

    我们在某资源网站上下载完成指定文件后,尤其是一些下载所需较高时长的大型文件,如何检验下载的文件是否完好,也即如何保证和原始网站上的资源一样.此时就要用到检验码的机制,一般文件的下载界面,通常都会给出此 ...

  8. 体验SUSE (附视频演示)

    操作动画演示 本文出自 "李晨光原创技术博客" 博客,谢绝转载!

  9. php学习笔记6

    PHP 字符串变量 PHP 中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量中. 在下面的实例中,我们创建一 ...

  10. Codeforces 559A Gerald&#39;s Hexagon 数三角形

    题意:按顺序给出一个各内角均为120°的六边形的六条边长,求该六边形能分解成多少个边长为1的单位三角形. 把单位三角形面积看做1,实际上就是求六边形面积.随便找六边形的三条互相不相邻的边,分别以这三条 ...