题目链接:

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. shell脚本安装ntp server 服务

    ##############################Deploy ntp server ######################## echo "start deploy ntp ...

  2. jQuery 获取DOM元素

    (function (window){ var arr=[]; var VP=function(selector,context){ return new VP.fn.init(selector,co ...

  3. HTML5之Canvas绘图(二) ——应用篇之七巧板

    1.canvas绘制七巧板-- <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  4. java String概述

    class StringDemo { public static void main(String[] args)  {  String s1 = "abc";//s1 是一个类类 ...

  5. IPv4(四)子网和子网掩码

    为了完成路由选择,每个数据链路(网络)都必须有一个惟一的地址: 另外,数据链路上的每台主机也必须有一个地址,这个地址不仅标识主机为一个网络成员,还可以把主机与网络上的其他主机区分开来. 粗放使用IPv ...

  6. 【虚拟机】WIN8.1系统虚拟机完全彻底删除

    一.首先删除注册表 用管理员身份打开CMD,输入F:\>VMware-workstation-full-12.5.5-5234757.exe/clean,根据自己的虚拟机安装文件的路径进行改变( ...

  7. Spring Cloud 微服务一:Consul注册中心

    Consul介绍 Consul is a service mesh solution providing a full featured control plane with service disc ...

  8. 跟着实例学习设计模式(6)-生成器模式builder(创建型)

    生成器模式是创建型设计模式. 设计意图:将一个复杂的类表示与其构造相分离,使得同样的构建过程可以得出不同的表示. 实例类图: IVehicleBuilder:抽象建造者.为创建一个Vehicle对象并 ...

  9. python scrapy爬虫框架

    http://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/tutorial.html scrapy 提取html的标签内容 from scrapy.selec ...

  10. Android-解决Fail to post notification on channel "null"的方法

    原文:https://blog.csdn.net/weixin_40604111/article/details/78674563 在sdk版本为25或25之前想在notification中添加一个点 ...