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. 如何在win10上同时安装python2和python3

    哎,其实本人已经用惯了python2,听说python3的语法有很多不一样的地方,那我之前写的算法改起来岂不是日了狗了吗?所以一直没改用python3.但是谷歌的那个TensorFlow,在windo ...

  2. 4455: [Zjoi2016]小星星|状压DP|容斥原理

    OrzSDOIR1ak的晨神 能够考虑状压DP枚举子集,求出仅仅保证连通性不保证一一相应的状态下的方案数,然后容斥一下就是终于的答案 #include<algorithm> #includ ...

  3. Dos图像复制成序列

    rem 输入1.png,在当前文件下复制.0000.png--0002.png rem 注:way2是不等待0001.png运行完就開始运行下一个了. rem 假设要等待上一个运行完后,再往下顺弃运行 ...

  4. Problem C: Celebrity Split

    题目描写叙述 Problem C: Celebrity Split Jack and Jill have decided to separate and divide their property e ...

  5. 关于使用strtok的一个小问题

    今天在弄一下啊小小程序的时候.报错,出现了问题.先看代码 int main(int argc, char* argv[]) { char *filename = "interface_ips ...

  6. java 三次样条插值 画光滑曲线 例子

    java 三次样条插值 画光滑曲线 例子 主要是做数值拟合,根据sin函数采点,取得数据后在java中插值并在swing中画出曲线,下面为截图  不光滑和光滑曲线前后对比:    代码: 执行类: p ...

  7. 【agc009b】Tournament

    Description 一场锦标赛有n个人,总共举办n-1次比赛,每次比赛必定一赢一输,输者不能再参赛.也就是整个锦标赛呈一个二叉树形式.已知一号选手是最后的胜者,以及对于i号选手(i>1)都知 ...

  8. ftp实现图片上传,文件也类似

    本来用得是easyui得控件 点击按钮实现选择图片得 ,但是老板非得要双击图片框实现上传代码....做个简单得记录 前端代码: 首先,<form>表单要加上 enctype="m ...

  9. xml数据文件上传至数据库

    上传xml文件数据到数据库思路:首先上传需要建校验xml字段的校验文件,然后程序根据后台写好的xml数据文件路径自动读取数据文件,再上传数据文件到数据库之前先根据校验文件校验上传的数据文件的字段是否合 ...

  10. hysbz 2243 染色(树链剖分)

    题目链接:hysbz 2243 染色 题目大意:略. 解题思路:树链剖分+线段树的区间合并,可是区间合并比較简单,节点仅仅要记录左右端点的颜色就可以. #include <cstdio> ...