题目链接:

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.

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

题意:

给一个数组,问把这些数全都变成一个数需要多少步操作,两种操作,一种是*2,一种是/2;

思路:

bfs找到一个数能变成的其它数和步数,所有的数操作完后,遍历1~1e5找到有多少个数能变成这个数(等于n的才符合要求),然后在这等于n的中间找到一个总操作数最小的那个;

AC代码:

/*
2014300227 558C - 8 GNU C++11 Accepted 202 ms 3756 KB
*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
typedef long long ll;
const double PI=acos(-1.0);
int n,a[N],num[N],vis[N],flag[N];
map<int,int>mp;
struct node
{
int x,step;
};
queue<node>qu;
queue<int>q;
void solve(int fx)
{
node ne;
ne.x=fx;
ne.step=;
qu.push(ne);
flag[fx]=;
while(!qu.empty())
{
int fy=qu.front().x,sum=qu.front().step;
num[fy]+=sum;
vis[fy]++;
if(fy*<=1e5&&flag[fy*]==)
{
ne.x=fy*;
ne.step=sum+;
qu.push(ne);
flag[fy*]=;
}
if(fy/>=&&flag[fy/]==)
{
ne.x=fy/;
ne.step=sum+;
qu.push(ne);
flag[fy/]=;
}
q.push(fy);
qu.pop();
}
while(!q.empty())flag[q.front()]=,q.pop();
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
solve(a[i]);
}
int ans=2e9;
for(int i=;i<=1e5;i++)
{
if(vis[i]==n)
{
ans=min(ans,num[i]);
}
}
cout<<ans<<endl; return ;
}

codeforces 558C C. Amr and Chemistry(bfs)的更多相关文章

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

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

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

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

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

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

  4. Codeforces 558C Amr and Chemistry 暴力 - -

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

  5. CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)

    Codeforces 558C 题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次. 分析: 计算每一个数的二进制公共前缀. 枚举法 ...

  6. 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/ ...

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

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

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

  9. CF 558 C. Amr and Chemistry 暴力+二进制

    链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...

随机推荐

  1. java代码实现输出指定以.java结尾的文件的绝对路径

    package 输出指定文件绝对路径; import java.io.File; /* * 需求:请大家把"E:\\JAVA语言"文件夹下全部的java结尾的文件的绝对路径给输出在 ...

  2. 处理字符串的一些C函数

    注意:以下函数都包含在ctype.h头文件中 1.isalpha函数 用来判断得到的参数是不是字母 #include<stdio.h> #include<ctype.h> in ...

  3. Mysql或者Hive数据行变成列

    对于mysql /  hive 再进行统计的时候假设须要行变成列,能够使用函数 CASE 字段a WHEN 值b THEN c [WHEN d THEN e]* [ELSE f] END 当字段a=值 ...

  4. request 解决中文乱码问题

    package request; import java.io.IOException;import javax.servlet.ServletException;import javax.servl ...

  5. [转]浅谈Flash Socket通信安全沙箱

    用过Flash socket的同学都知道,Flash socket通讯有安全沙箱问题.就是在Flash Player发起socket通信时,会向服务端获取安全策略,如果得不到服务端响应,flash将无 ...

  6. 史上最浅显易懂的Git教程1

    工作区(Working Directory)就是你在电脑里能看到的目录, 工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库. Git的版本库里存了很多东西,其中最重要的就是称为stag ...

  7. Oracle 10g ORA-12154: TNS: could not resolve the connect identifier specified 问题解决! 我同事遇到的问题。 username/

    Oracle 10g ORA-12154: TNS: could not resolve the connect identifier specified 问题解决! 我同事遇到的问题. userna ...

  8. 视频服务之ffmpeg部署

    FFmpeg介绍 FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序. 采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案. 它包含了非常先进 ...

  9. SQLite集成与用法

    本文转载至 http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/fra ...

  10. python 基础 2.6 for 循环 和if循环 中break

    python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...