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. Quartz.Net 使用心得(一)

    最近工作内容与定时任务相关,在实际使用Quartz过程中,有两个小问题较为困扰. 一.多个Trigger如何触发一个Job. 比如上下班打卡时推送消息,上班时间为9:30,打卡提醒时间为9:20较好. ...

  2. HDU 5045 DP+状压

    2014 ACM/ICPC Asia Regional Shanghai Online 给出N个人做M道题的正确率,每道题仅仅能由一个人做出,而且当全部人都做出来且仅做出一道题时,做过题的人才干够继续 ...

  3. jQuery中实现自己定义方法的扩展

    JQuery包装器提供了大量的方法.能够再页面中直接使用.可是.没有 不论什么一个库能够满足全部的需求.所以.JQuery库提供了丰富的扩展功能 .以禁用一组表单元素为例.看看怎么简单有效的在JQue ...

  4. POJ3249 Test for Job(拓扑排序+dp)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10137   Accepted: 2348 Des ...

  5. [SCOI 2005] 栅栏

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1082 [算法] 首先二分“最多得到的符合条件的木板数”,检验时可以使用DFS,但是, ...

  6. C#读出文本文件内容,遍历数组筛选出 含有汉字对应的拼音字符

    情景描述:由于任务需要,现有一用户表数据,用户名 字段 在新增用户时,输入中文和拼音两种,先要区分同时含有中文和拼音字母的用户名.由于数据很多,可以通过一段代码完成查询: 前提:在阅读本文之前可以先了 ...

  7. 2.linux系统命令详解

    1 shell shell:命令解释器,根据输入的命令执行相应命令. 1.1 shell家族 察看当前系统下有哪些shell: cat /etc/shells 察看当前系统正在使用的shell ech ...

  8. ffmpeg键盘命令响应程序详解

    一.对终端进行读写 当一个程序在命令提示符中被调用时, shell负责将标准输入和标准输出流连接到你的程序, 实现程序与用户间的交互.   1. 标准模式和非标准模式 在默认情况下, 只有用户按下回车 ...

  9. Java基础——选择排序、冒泡排序

    1.选择排序 原理是直接从待排序数组里选择一个最小(或最大)的数字,每次都拿一个最小数字出来, 顺序放入新数组,直到全部拿完 代码演示: public class Test3 { public sta ...

  10. windows phone LongListSelector加载下一页

    LongListSelector利用ListHeader.ListFooter加载上一页和下一页XAML代码: <phone:LongListSelector> <phone:Lon ...