C. Amr and Chemistry
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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.

Sample test(s)
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.

题意:给定n长的序列,每次能够选一个数 让其 乘以2 或者 除以2。问至少操作多少次使得全部数相等。

点击打开链接

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> using namespace std; int a[1000050];
int num[1000010];
int v[10000060];
int maxx;
int n; int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(num,0,sizeof(num));
memset(v,0,sizeof(v));
maxx = -1;
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
v[a[i]]++;
if(maxx<a[i])
{
maxx = a[i];
}
}
for(int i=0; i<n; i++)
{
int t = a[i];
int cnt = 0;
while(t<maxx)
{
t = t<<1;
cnt++;
v[t]++;
num[t] += cnt;
}
cnt = 0;
int pp;
t = a[i];
while(t>0)
{
int flag = 0;
if(t%2 == 1 && t!=1)
{
flag = 1;
}
t = t>>1;
cnt++;
v[t]++;
num[t] += cnt;
if(flag == 1)
{
pp = t;
int pcnt = cnt;
while(pp<maxx)
{
pp = pp<<1;
pcnt++;
v[pp]++;
num[pp] += pcnt;
} }
}
}
int minn = 9999999;
for(int i=0; i<=maxx; i++)
{ if(v[i] == n && minn > num[i])
{
minn = num[i];
}
}
printf("%d\n",minn);
}
return 0;
}

C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)的更多相关文章

  1. Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力

    C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...

  2. Codeforces Round #312 (Div. 2) C.Amr and Chemistry

    Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...

  3. Codeforces Round #312 (Div. 2) ABC题解

    [比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...

  4. Codeforces Round #312 (Div. 2)

    好吧,再一次被水题虐了. A. Lala Land and Apple Trees 敲码小技巧:故意添加两个苹果树(-1000000000, 0)和(1000000000, 0)(前者是位置,后者是价 ...

  5. Codeforces Round #312 (Div. 2)B. Amr and The Large Array 暴力

    B. Amr and The Large Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  6. Codeforces Round #312 (Div. 2) B.Amr and The Large Array

    Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. ...

  7. B. Amr and The Large Array(Codeforces Round #312 (Div. 2)+找出现次数最多且区间最小)

    B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...

  8. Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力

    A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...

  9. Codeforces Round #312 (Div. 2) A.Lala Land and Apple Trees

    Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. ...

随机推荐

  1. thrift 版本不一致导致 @Override 报错

    thrift 版本不一致导致 @Override 报错 学习了:http://blog.csdn.net/antony1776/article/details/78920888 版本不一致导致的: 在 ...

  2. Android开发之——编码规范

    1. 前言 这份文档参考了 Google Java 编程风格规范和 Google 官方 Android 编码风格规范.该文档仅供参考,只要形成一个统一的风格,见量知其意就可. 2. 源文件基础 2.1 ...

  3. 关于ZipOupputStream添加压缩包常见问题

    其实园子压缩解压缩的方法很多,ZipOupputStream这个类的说明很多,我这边也是从网上找的代码,但是我在压缩的时候遇到了常见的两个问题,第一个就是压缩的时候读取压缩包报该压缩包已经在另一个进程 ...

  4. 【Python】python网络协议

    套接字是常见的低级别的网络通讯协议,在此基础上,还有很多其他的网络通讯协议.用于实现client-server的网络互联,以下对这些协议做一个简单的介绍. 1.文件传输 FTP:文件传输协议.能够上传 ...

  5. 2016.03.28,英语,《Vocabulary Builder》Unit 07

    vis: comes from a Latin verb meaning 'see'. vision: ['vɪʒn] n. 视觉,先见之明,光景,视力,眼力,幻想,影像 vt. 幻想. ; vid- ...

  6. apiCloud中的数据库操作mcm-js-sdk的使用

    1.引入js <!-- 引入mcm-js-sdk Begin --> <script type="text/javascript" src="../pl ...

  7. notepad++ 查找引用(Find Reference)(适用于c c++及各类脚本比如lua、python等)

    在程序开发过程中,程序员经常用到的一个功能就是查找引用(Find Reference),Visual Studio里面的对应功能是“查找所有引用”(Find All References). 我在使用 ...

  8. Swift 字符串的分割

    componentsSeparatedByString 就是根据 传递的 一个字符,进行字符串的一个分割 // 分割字符串 - > let sourceArray = source.compon ...

  9. POJ 2392 DP

    题目大意:有一头奶牛要上太空,他有很多种石头,每种石头的高度是hi,但是不能放到ai之上的高度,并且这种石头有ci个 将这些石头叠加起来,问能够达到的最高高度. 题意转载自:http://blog.c ...

  10. Git不需重复输入账号和密码的方法

    1. 打开 TortoiseGit 附带工具 Puttygen(PuTTY Key Generator)  C:\Program Files\TortoiseGit\bin\puttygen.exe. ...