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. 工具-常用VS插件

    工欲善其事,必先利其器,没有好的工具,怎么能高效的开发出高质量的代码呢?本文为各ASP.NET 开发者介绍一些高效实用的工具,涉及SQL 管理,VS插件,内存管理,诊断工具等,涉及开发过程的各个环节, ...

  2. C#中的文本乱码问题

    文本乱码问题 //提供一种解决C#文本乱码的解决思路 //写入使用: string str; str = this.menu.Text; string fname = Application.Star ...

  3. Java 序列化 和 反序列--by Vincent

    序列化: Java 提供了一种把对象序列化的机制. 即把一个对象可以被表示为一个字节序列.然后可以写入文件保存. 字节序列包括:      该对象的数据.有关对象的类型的信息和存储在对象中数据的类型. ...

  4. 一步一步跟我学习hadoop(5)----hadoop Map/Reduce教程(2)

    Map/Reduce用户界面 本节为用户採用框架要面对的各个环节提供了具体的描写叙述,旨在与帮助用户对实现.配置和调优进行具体的设置.然而,开发时候还是要相应着API进行相关操作. 首先我们须要了解M ...

  5. iOS开发之autoLayout constraint

    前言 ios设备的尺寸越来越多,针对一款app可能要适配到多种设备.多种尺寸.所以.我们期望我们的app可以autoLayout.本文主要介绍在Xcode中使用constraint.未来会不定期对此文 ...

  6. android一个弹出菜单的动画(一)

    先上效果图: 先写Layout文件: <?xml version="1.0" encoding="utf-8"? > <RelativeLay ...

  7. java 正则表达式(一)

    1匹配验证-验证Email是否正确 Java | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static void main(String[] args)  ...

  8. vmvare如何安装xp虚拟机

    http://jingyan.baidu.com/article/a681b0ded8e25e3b19434640.html 一直以来,许多的朋友都不熟悉怎么安装在虚拟机上装windows系统 200 ...

  9. $.ajax 和$.post的区别

    https://blog.csdn.net/weixin_39709686/article/details/78680754

  10. Bootstrap表格内容居中

    1.<th style='text-align: center;'>host</th> 水平居中 2.<td rowspan=$row_host1 style='vert ...