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. nodejs-Sream

    Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请求的request 对象就是一个 Stream,还有stdout(标准输出). Node.js,Str ...

  2. Objective-C - 改变NSMutableArray的特定元素

    NSMutableArray改动元素, 使用-insertObject: atIndex: 和-replaceObjectAtIndex: withObject: 都能够, 即通过插入(insert) ...

  3. Spring-SpringJdbcTemlate配置介绍

    使用spring的jdbcTemplate进一步操作JDBC 一.普通配置  SpringJdbcTemplate连接数据库并操作数据 1.applicationContext.xml 1.1 建立D ...

  4. Android接口和框架学习

    Android接口和框架学习 缩写: HAL:HardwareAbstraction Layer.硬件抽象层 CTS:CompatibilityTest Suite,兼容性測试套件 Android让你 ...

  5. linux:共享内存

    #include <sys/ipc.h> #include <sys/shm.h> #include <string.h> #include <stdio.h ...

  6. UI组件之AdapterView及其子类(五)ListView组件和ListActivity

    ListView组件是一个显示组件,继承AdapterView基类,前面已经介绍了分别使用ArrayAdapter,SimpleAdapter,扩展BaseAdapter来为LisView提供列表项h ...

  7. 编译Redis系统提示缺少gcc,可以使用yum进行安装:

    yum -y install gcc yum -y install gcc-c++ yum install make -- 或者 yum groupinstall "Development ...

  8. Team Services 自动化部署项目

    一.创建Team Services账号 直接使用vs的账号即可,TS上集成了Git.把项目导入到Git中. 使用前提:有自己的服务器,把项目自动化部署到服务器上. 二.创建一个新的定义 三.选择种类( ...

  9. SQL常用函数集锦

    ..STUFF()用另一子串替换字符串指定位置.长度的子串.STUFF (<character_expression1>, <start_ position>, <len ...

  10. @DateTimeFormat无效原因

    一般都是使用@DateTimeFormat把传给后台的时间字符串转成Date,使用@JsonFormat把后台传出的Date转成时间字符串,但是@DateTimeFormat只会在类似@Request ...