codeforces 558C C. Amr and Chemistry(bfs)
题目链接:
1 second
256 megabytes
standard input
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?
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 one integer the minimum number of operations required to make all the chemicals volumes equal.
3
4 8 2
2
3
3 5 6
5
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)的更多相关文章
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 558/C Amr and Chemistry(数论+位运算)
题目链接:http://codeforces.com/problemset/problem/558/C 题意:把n个数变成相同所需要走的最小的步数易得到结论,两个奇数不同,一直×2不可能有重叠枚举每个 ...
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...
- Codeforces 558C Amr and Chemistry 暴力 - -
点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
Codeforces 558C 题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次. 分析: 计算每一个数的二进制公共前缀. 枚举法 ...
- 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/ ...
- Codeforces Round #312 (Div. 2) C.Amr and Chemistry
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...
- 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 ...
- CF 558 C. Amr and Chemistry 暴力+二进制
链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...
随机推荐
- iOS中UDP的使用
// // ViewController.m // UDPDemo // // Created by qianfeng01 on 15-8-13. // Copyright (c) 2015年 ...
- 2014acm亚洲区域赛陕西赛总结
这次是第一次出来到外面比赛,一切都是非常新鲜的,带着新鲜来到了古城西安.首先感觉就是志愿者一点都不热情.一副爱理不理的,这不是有违我大西北人的热情好客么. 直接说比赛吧. 第一天热身赛,出了两道非常水 ...
- Centos 7 远程桌面客户端
在centos下面要远程连接windows,有人说用rdesktop,但是好像centos 7没有,对从源代码编译也不大感兴趣. 幸好还有人提醒, https://geekblood.com/2014 ...
- LightOJ 1068 Investigation (数位dp)
problem=1068">http://www.lightoj.com/volume_showproblem.php?problem=1068 求出区间[A,B]内能被K整除且各位数 ...
- JVM完全指南
JVM完全指南 一:虚拟机内存图解 JAVA程序运行与虚拟机之上,运行时需要内存空间.虚拟机执行JAVA程序的过程中会把它管理的内存划分为不同的数据区域方便管理. 虚拟机管理内存数 ...
- zeroMQ研究(转)
偶尔一个机会,了解了下zeroMQ消息队列. 1 ZeroMQ概述 ZeroMQ是一种基于消息队列的多线程网络库,其对套接字类型.连接处理.帧.甚至路由的底层细节进行抽象,提供跨越多种传输协议的套接 ...
- 查看SELinux状态并关闭SELinux
SELinux(Security-Enhanced Linux)是Linux上最杰出的新安全子系统.在linux内核级别上提供了一个灵活的强制访问控制系统(MAC),这个强制访问控制系统是建立在自由访 ...
- redis问题接囧办法及经验
转自:https://my.oschina.net/freegeek/blog/324410 1.redis持久化,来自官方说明 如何选择使用哪种持久化方式? 一般来说, 如果想达到足以媲美 Post ...
- ASP.NET动态网站制作(4)--css(3)
前言:这节课主要运用前面所学的知识写三个例子,并且学习浏览器兼容性的解决方法. 内容: 例子1:一个关于列表的例子 html代码: <!DOCTYPE html PUBLIC "-// ...
- 转载 ---原生和H5交互挺多的,最近也有朋友再问。这儿我写个简单的例子给大家 直接贴代码 js的
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...