链接:http://codeforces.com/problemset/problem/558/C

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.

题意:

给你若干个数。

对每一个数都能够进行除或者乘的操作。  3/2=1

问 最少 多少步操作。能够让全部数字都相等。

做法:

首先假设都变成一个数字。那这个数字肯定是小于等于这些数字里的最大的那个的。

这里能够把每一个数字看成二进制。

11(D)

1101(2)

所以事实上能够枚举随意一个数。

随意一个二进制能够往左移若干位,得到一个新的数字。

出现奇数次数最多是log(n)。向左移动最多也是log(n)  所以复杂度是log^2(n)*n

或者往右移。

能够知道假设奇数往右移时,二进制会少一个1,所以此时向左移会出现的数字都是之前没出现过的。

把每一个出现的数字记录步数,记录出现次数,最后统计每一个出现次数到达n的数字的最小步数和。

int ci[101000];
int buhe[101000];
int main()
{
int n;
int lim=100000;
while(scanf("%d",&n)!=EOF)
{
memset(ci,0,sizeof ci);
memset(buhe,0,sizeof buhe);
for(int i=0;i<n;i++)
{
int num;
scanf("%d",&num);
int flag=1;
int bu=0;
while(num)
{
if(flag)
{
int tt=bu+1;
int num2=num*2;;
while(num2<=lim)
{
ci[num2]++;
buhe[num2]+=tt; num2*=2;
tt++;
}
flag=0;
}
ci[num]++;
buhe[num]+=bu;
bu++;
if(num%2==0)
num/=2;
else
{
flag=1;
num/=2;
}
}
}
int minn=999999999;
for(int i=1;i<=lim;i++)
{
if(ci[i]==n)
minn=min(minn,buhe[i]);
}
printf("%d\n",minn);
}
return 0;
}

CF 558 C. Amr and Chemistry 暴力+二进制的更多相关文章

  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 558C Amr and Chemistry 暴力 - -

    点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...

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

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

  4. C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)

    C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry

    C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...

  6. Amr and Chemistry

    C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. 【23.39%】【codeforces 558C】Amr and Chemistry

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

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

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

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

随机推荐

  1. twemproxy源码分析2——守护进程的创建

    twemproxy源码中关于守护进程的创建实现得比较标准,先贴出代码来,然后结合一些资料来分析和列举一些实现守护进程的常用方法,不过不得不说twemproxy的实现确实是不错的,注释都写在了代码中,直 ...

  2. oracle 创建表空间及oracle 11g表空间之最大最小

    /*分为四步 *//*第1步:创建临时表空间 */create temporary tablespace emaoyi_temp tempfile 'D:\app\Administrator\prod ...

  3. Linux 设备驱动的固件载入

    作为一个驱动作者, 你可能发现你面对一个设备必须在它能支持工作前下载固件到它里面. 硬件市场的很多地方的竞争是如此得强烈, 以至于甚至一点用作设备控制固件的 EEPROM 的成本制造商都不愿意花费. ...

  4. 【转】Oracle Sys和system用户、sysdba 和sysoper系统权

    一:最重要的区别,存储的数据的重要性不同 [sys]所有oracle的数据字典的基表和视图都存放在sys用户中,这些基表和视图对于oracle的运行是至关重要的,由数据库自己维护,任何用户都不能手动更 ...

  5. linux命名对文件的读写和退出

    vi xxx.txt 打开就能看到里面的内容.按 i 进入编辑模式,然后就可以输入内容了,也可以移动光标到你要删除内容的位置按删除键来删除内容.编辑完后可以按 Esc(键盘左上角) 进入命令模式.然后 ...

  6. tomocat解决乱码问题

    使用Tomcat进行JSP开发最头疼的莫过于中文乱码问题了,总结Tomcat乱码问题出现的原因必须明白以下几点: 1.Tomcat一般总是默认使用ISO-8859-1作为字符编码方式的.所以,除非你在 ...

  7. 解决java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver问题

    今天在做项目的时候突然遇到解决java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver问题,知道是j ...

  8. Android 消息处理源代码分析(2)

    Android 消息处理源代码分析(1)点击打开链接 继续接着分析剩下的类文件 Looper.java public final class Looper { final MessageQueue m ...

  9. java读取配置文件(转)

    转载:http://blog.csdn.net/gaogaoshan/article/details/8605887 java 4种方式读取配置文件 + 修改配置文件     方式一:采用Servle ...

  10. Java序列化(转载)

    引用自:http://developer.51cto.com/art/201506/479979_all.htm 关于 Java 对象序列化您不知道的 5 件事 数年前,当和一个软件团队一起用 Jav ...