【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 ...
随机推荐
- Android基础新手教程——3.8 Gestures(手势)
Android基础新手教程--3.8 Gesture(手势) 标签(空格分隔): Android基础新手教程 本节引言: 周六不歇息,刚剪完了个大平头回来.继续码字~ 好的,本节给大家带来点的是第三章 ...
- js-YDUI 移动端解决方案
/** * YDUI 可伸缩布局方案 * rem计算方式:设计图尺寸px / 100 = 实际rem 例: 100px = 1rem */ !function (window) { /* 设计图文档宽 ...
- 失去偏执的苹果会如何?Android 会一统天下吗?
第 一次听到Apple你绝对不会想到和手机相关.但就是这样一个不相关的东西,彻彻底底的改变了我们这个世界.说起苹果那就得必须说到乔帮主,由于苹果仅仅属于Steve jobs. 乔布斯天生聪颖.初二測得 ...
- SQL查询练习二(From LeetCode)
请选用MySQL进行测试. 1.将男性和女性的工资互换(E) 思路:使用case when进行条件判断,在使用update进行修改 update salary set sex = case sex w ...
- 自己动手开发jQuery插件全面解析 jquery插件开发方法
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- 水题ing
T1: https://www.luogu.org/problemnew/show/P1724幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆 ...
- WebService学习总结(5)——WebService常见开发框架比较
在SOA领域,我们认为Web Service是SOA体系的构建单元(building block).对于服务开发人员来说,AXIS和CXF一定都不会陌生.这两个产品都是Apache孵化器下面的Web ...
- Serializable中的serialVersionUID到底有啥用
最近在研究跨进程通信的问题,于是又再一次研究了,我们熟悉而又陌生的Serializable接口. 那么好,做过Java开发的朋友肯定对这个接口不陌生吧,Java中就是通过这个接口,来实现了序列化和反序 ...
- win7管理工具不可用
看看这个路径的文件夹是否还在C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools如果有缺失的文件夹就自己新 ...
- 每日技术总结:Yarn和Npm大PK
今天想用npm安装vue-cli@2.9 npm install --global vue-cli@2.9 卡半天,安装不成功,清空缓存,换taobao源重来,还是一样. 无奈之下换yarn yarn ...