CF 558 C. Amr and Chemistry 暴力+二进制
链接:http://codeforces.com/problemset/problem/558/C
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.
题意:
给你若干个数。
对每一个数都能够进行除或者乘的操作。 3/2=1
问 最少 多少步操作。能够让全部数字都相等。
做法:
首先假设都变成一个数字。那这个数字肯定是小于等于这些数字里的最大的那个的。
这里能够把每一个数字看成二进制。
如
11(D)
1101(2)
所以事实上能够枚举随意一个数。
随意一个二进制能够往左移若干位,得到一个新的数字。
出现奇数次数最多是log(n)。向左移动最多也是log(n) 所以复杂度是log^2(n)*n
或者往右移。
能够知道假设奇数往右移时,二进制会少一个1,所以此时向左移会出现的数字都是之前没出现过的。
把每一个出现的数字记录步数,记录出现次数,最后统计每一个出现次数到达n的数字的最小步数和。
int ci[101000];
int buhe[101000];
int main()
{
int n;
int lim=100000;
while(scanf("%d",&n)!=EOF)
{
memset(ci,0,sizeof ci);
memset(buhe,0,sizeof buhe);
for(int i=0;i<n;i++)
{
int num;
scanf("%d",&num);
int flag=1;
int bu=0;
while(num)
{
if(flag)
{
int tt=bu+1;
int num2=num*2;;
while(num2<=lim)
{
ci[num2]++;
buhe[num2]+=tt; num2*=2;
tt++;
}
flag=0;
}
ci[num]++;
buhe[num]+=bu;
bu++;
if(num%2==0)
num/=2;
else
{
flag=1;
num/=2;
}
}
}
int minn=999999999;
for(int i=1;i<=lim;i++)
{
if(ci[i]==n)
minn=min(minn,buhe[i]);
}
printf("%d\n",minn);
}
return 0;
}
CF 558 C. Amr and Chemistry 暴力+二进制的更多相关文章
- 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 558C Amr and Chemistry 暴力 - -
点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...
- codeforces 558/C Amr and Chemistry(数论+位运算)
题目链接:http://codeforces.com/problemset/problem/558/C 题意:把n个数变成相同所需要走的最小的步数易得到结论,两个奇数不同,一直×2不可能有重叠枚举每个 ...
- 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 ...
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...
- Amr and Chemistry
C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Round #312 (Div. 2) C.Amr and Chemistry
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...
- 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 ...
随机推荐
- WebRTC编译具体介绍
WebRTC编译具体介绍--记录+转载 原文地址:http://blog.csdn.net/temotemo/article/details/7056581 WebRTC编译 本人环境: 操作系统:X ...
- 【shell】使用 /dev/null crontab
1.linux组成kernel.shell.工具程序有sh.bash 一个例子 !#/bin/bash echo '' 执行之前chmod +x 执行./ 2.一个小窍门 cp /dev/null / ...
- C++基础细节2
1.关于引用和指针 概念上,引用(&)并不是对象,而是一个已经存在的对象的别名:引用不可以重新绑定到另外一个对象,因此引用必须初始化.(类比const,一经定义就不能修改,所以必须初始化,是同 ...
- kaggle比赛之youtube视频分类示例
1.训练模型:建bucket,建job,提交运行. BUCKET_NAME=gs://${USER}_yt8m_train_bucket_logisticmodel # (One Time) Crea ...
- crontab读不来环境变量
无法识别java nohup: failed to run command `java': No such file or directory 那么在shell中加两行即可解决 . /etc/prof ...
- 隐式意图调用系统自带组件的各种Uri总结
调用系统应用解析(必需要加各自使用的权限) android intent 隐式意图和显示意图(activity跳转) 显示意图要求必须知道被激活组件的包和class 隐式意图仅仅须要知道跳转acti ...
- AWS系列-申请Redis
1.1 打开aws控制台,可以直接搜索redis 1.2 进入redis控制面板 点击启动缓存集群(这个只是启动创建的意思,不是启动下面创建好的node.我也不懂为啥翻译过来是这个意思...) 1.3 ...
- 你一定喜欢看的 Webpack 2.× 入门实战
from:https://www.jianshu.com/p/b83a251d53db?utm_campaign=maleskine&utm_content=note&utm_medi ...
- 一起talk C栗子吧(第一百二十七回:C语言实例--查看main函数的參数)
各位看官们,大家好,上一回中咱们说的是static关键字的样例,这一回咱们说的样例是:查看main函数的參数.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们.我们在第五十七回中介绍过mai ...
- django用户认证系统——登录4
用户已经能够在我们的网站注册了,注册就是为了登录,接下来我们为用户提供登录功能.和注册不同的是,Django 已经为我们写好了登录功能的全部代码,我们不必像之前处理注册流程那样费劲了.只需几分钟的简单 ...