【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
【题目链接】:http://codeforces.com/contest/558/problem/C
【题解】
先搞出来每个数字能变成什么数字(最大到1e5就好,不放心就加大一点);
数字能够乘2也能除2;
但不是猛地一直乘或一直除;
如果数字是
3
则3/2=1
但是这个时候
2 4 8 16都能达到了;
即1*2,1*2*2,1*2*2*2….
则除的时候要多判断一下这个数是不是奇数;
如果是奇数它除完之后还能一直乘.
因为最后要求所有的数字都变成同一个数字;
则我们在处理每一个数字到达的数字的时候,可以直接累加这个数字要经过多少步到达;
最后O(N)枚举求最小值就好;(n个数字都能变成这个数字的话);
用map会T…
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define pri(x) printf("%d",x)
#define prl(x) printf("%I64d",x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 1e5+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n;
int a[MAXN];
int dic[MAXN],dic3[MAXN];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rei(a[i]);
rep1(i,1,n)
{
int step = 0;
int now = a[i];
dic[now]++;
dic3[now]+=step;
now<<=1;
while (now <= 1e5)
{
dic[now]++;
step++;
dic3[now]+=step;
now<<=1;
}
now = a[i];
step = 0;
while (now >0)
{
if (now&1)
{
int tnow = now>>1;
if (tnow<=0) break;
int tstep = step+1;
tnow <<=1;
tstep++;
while (tnow <= 1e5)
{
dic[tnow]++;
dic3[tnow]+=tstep;
tstep++;
tnow<<=1;
}
}
now>>=1;
if (now <=0) break;
step++;
dic[now]++;
dic3[now]+=step;
}
}
int ans = 21e8;
rep1(i,0,1e5)
if (dic[i]==n)
ans = min(ans,dic3[i]);
pri(ans);
return 0;
}
【23.39%】【codeforces 558C】Amr and Chemistry的更多相关文章
- codeforces 558C C. Amr and Chemistry(bfs)
题目链接: C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input st ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【23. 合并K个排序链表】【困难】【优先队列/堆排序】
合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6] 输出: 1->1-> ...
- codeforces 558/C Amr and Chemistry(数论+位运算)
题目链接:http://codeforces.com/problemset/problem/558/C 题意:把n个数变成相同所需要走的最小的步数易得到结论,两个奇数不同,一直×2不可能有重叠枚举每个 ...
- 【39.66%】【codeforces 740C】Alyona and mex
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【20.23%】【codeforces 740A】Alyona and copybooks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【23.33%】【codeforces 557B】Pasha and Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【30.23%】【codeforces 552C】Vanya and Scales
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- [React] Create a queue of Ajax requests with redux-observable and group the results.
With redux-observable, we have the power of RxJS at our disposal - this means tasks that would other ...
- Tomcat之——配置项目有虚拟路径
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47024863 非常easy,在Tomcat的Server.xml文件里的Host节 ...
- hdu5308 I Wanna Become A 24-Point Master(构造)
题目:pid=5308" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=5308 题意:给定 ...
- BZOJ2882
传送门:BZOJ2882(权限题) 最小表示法的模板. 传送门:周神论文 代码上的小细节见下. #include <cstdio> #include <cstdlib> #in ...
- virmon防火墙64位正式版(暂定)公布
ChangeLog: 2015-06-2564位版本号签名问题临时得到解决.还要致谢一下某位黑客. 支持版本号x64 Windows Vista.7.8.8.1以上等.个人仅仅在Windows7上做了 ...
- android sdk 镜象网站
因为一些原因.Google相关非常多服务都无法訪问,所以在非常多时候我们SDK也无法升级,当然通过技术手段肯定能够解决,可是比較麻烦,并且下载速度也不怎么样. 这里笔者介绍一个国内的Android镜像 ...
- gridview-selector的设置
其实它是跟listview相似的,如果你看下它跟listview的继承关系,就很容易理解了 public class GridView extends AbsListView { /** * Disa ...
- php实现合并多个数组
php实现合并多个数组 一.总结 1.就是想c++和java里面合并数组那么简单,就是把多个数组的值赋值个一个啊,很简单 二.代码 合并多个数组,不用array_merge(),题目来于论坛. 思路: ...
- qtcreator配置
如果是做嵌入式开发,那么宿主机使用的交叉编译的QT版本是必须和目标板上使用的qt版本一致的. 而且在路径上,如果qt使用的库如果不是放在目标板默认的连接库路径的话, 那么目标板qt程序就会到配置编译Q ...
- JS数组排序技巧汇总(冒泡、sort、快速、希尔等排序)
本文实例总结了JS数组排序技巧.分享给大家供大家参考,具体如下: 1.冒泡排序 var temp = 0; for (var i = 0; i < array.length; i++) { fo ...