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 ...
随机推荐
- JS——简单的正则表达式验证
<!-- 用户注册:结构层:html;表现层:css;行为层:javascript; html利用ul,li来构造: 注意事项:1.每个Input都要有相应的id,这是在js中去调用的. 2.& ...
- 类加载器与Web容器
在关于类加载器中已经介绍了Jvm的类加载机制,然而对于运行在Java EE容器中的Web应用来说,类加载器的实现方式与一般的Java应用有所不同.不同的Web容器的实现方式也会有所不同. Tomcat ...
- Flex桌面AIR软件日志添加
日志包装类 package log { import com.adobe.air.logging.FileTarget; import flash.filesystem.File; import fl ...
- Paxos发展、算法原理
Paxos 发展史 Leslie Lamport所提出的Paxos算法是现代分布式系统中的一项重要的基础性技术,得到广泛的应用. Paxos的整个发展过程大概可以分为三个阶段: 第一阶段:萌芽期,大致 ...
- hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流
/** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由 ...
- 在grub的rescue模式修复linux引导
今天在windows 10系统收到系统更新通知,没看清楚就手贱点了马上更新.以为只是像那些普通更新一样重启一下更新就完了,万万没想到这个是覆盖更新,也就是说这是一个全新的系统更新而不是系统补丁.在安装 ...
- pl/sql 实例精解 06
1. 简单循环 1: LOOP 2: statement1; 3: statement2; 4: EXIT WHEN condition; 5: END LOOP; 6: statement3; 也可 ...
- MFC绘图相关GDI工具对象和函数介绍
在利用MFC进行界面编程时,除了需要熟悉各种类型控件的操作外,还会经常遇到图形绘制和显示的问题,比如时频分析界面.图像处理界面等.处理这些软件界面开发问题时,不可避免地需要用到一系列GDI工具对象和相 ...
- 第一百四十二节,JavaScript,封装库--运动动画和透明度动画
JavaScript,封装库--运动动画和透明度动画 /** yi_dong_tou_ming()方法,说明 * * yi_dong_tou_ming()方法,将一个元素,进行一下动画操作 * 1,x ...
- 消息队列ipc的一些设置
Linux IPC 参数设定- 命令方式: echo 80 > /proc/sys/vm/overcommit_ratio, etc MSGMNB 每个消息队列的最大字节限制. MSGMNI 整 ...