题目链接:

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. React学习之redux

    在阅读本文之前,希望大家对以下知识点能提前有所了解并且上好厕所(文章有点长): 状态提升的概念 react高阶组件(函数) es6基础 pure 组件(纯函数) Dumb 组件 React.js的co ...

  2. 【Android】怎样写一个JsBridge

    JsBridge 简单介绍 Android JsBridge 就是用来在 Android app的原生 java 代码与 javascript 代码中架设通信(调用)桥梁的辅助工具. 原文地址点这里 ...

  3. Django之forms表单类

    Form表单的功能 自动生成HTML表单元素 检查表单数据的合法性 如果验证错误,重新显示表单(数据不会重置) 数据类型转换(字符类型的数据转换成相应的Python类型) 1.创建Form类 from ...

  4. Sping Boot入门到实战之实战篇(二):一些常用功能的Spring Boot Starters

    包含功能 阿里云消息服务MNS 阿里云消息队列服务(即时消息.延迟消息.事务消息) AOP日志 基于MyBatis通用Mapper及DRUID的数据库访问 dubbo支持 错误处理 七牛图片服务 re ...

  5. linux SPI驱动——spidev之deive(五)

    1.定义board设备 1: struct spi_board_info { 2: /* the device name and module name are coupled, like platf ...

  6. JavaScript中setInterval用法

    setInterval动作的作用是在播放动画的时,每隔一定时间就调用函数,方法或对象.可以使用本动作更新来自数据库的变量或更新时间显示. setInterval动作的语法格式如下:setInterva ...

  7. H2 database 操作操作内存表

    本例开发工具为 NetBeans,使用b2前提安装jdk. 第一步:在官网下载驱动包 :http://www.h2database.com ,本例版本为: h2-1.4.192.jar 第二步:安装开 ...

  8. Win10上Python3通过pip安装时出现UnicodeDecodeError

    http://blog.csdn.net/qq_33530388/article/details/68933201 解决方法: 打开 c:\program files\python36\lib\sit ...

  9. nagios-plugins安装报错--with-mysql: no

    --with-mysql: no 解决方法 yum安装mysql-devel yum install mysql-devel

  10. 九度OJ 1058:反序输出 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8454 解决:3042 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可能包含多组用例,每组用例 ...