博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF 558C(Amr and Chemistry-构造法)
阅读量:5167 次
发布时间:2019-06-13

本文共 3149 字,大约阅读时间需要 10 分钟。

C. Amr and Chemistry
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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?

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.

Sample test(s)
input
34 8 2
output
2
input
33 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.

将每一个数写为2进制。计算公共前缀

答案必为 [最长公共前缀(LCS)]+00...00

证:

若LCS后有1

则每一个数都要在对应位置有1,

故LCS可增长。矛盾

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i
=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (100000+10)typedef long long ll;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}int n,a[MAXN],a2[MAXN],h[MAXN]={0},q[MAXN]={0};int main(){ freopen("C.in","r",stdin);// freopen(".out","w",stdout); cin>>n; For(i,n) scanf("%d",&a[i]); sort(a+1,a+1+n); memcpy(a2,a,sizeof(a)); int p=a[1]; For(i,n) { while (p^a[i]) { if (p
>=1; else p>>=1; } } memcpy(a,a2,sizeof(a)); For(i,n) { while (a[i]^p) { h[i]--; if (a[i]&1) q[i]=h[i]; a[i]>>=1; } } ll ans=INF; Rep(t,100) { ll p=0; For(i,n) { if (q[i]==0||h[i]+t<=q[i]) p+=abs(h[i]+t); else p+=abs(q[i])+abs(q[i]-(h[i]+t)); } ans=min(ans,p); } cout<
<

转载于:https://www.cnblogs.com/yfceshi/p/6927520.html

你可能感兴趣的文章
C++ 面向对象 类成员函数this指针
查看>>
NSPredicate的使用,超级强大
查看>>
自动分割mp3等音频视频文件的脚本
查看>>
判断字符串是否为空的注意事项
查看>>
布兰诗歌
查看>>
js编码
查看>>
Pycharm Error loading package list:Status: 403错误解决方法
查看>>
steps/train_sat.sh
查看>>
转:Linux设备树(Device Tree)机制
查看>>
iOS 组件化
查看>>
(转)Tomcat 8 安装和配置、优化
查看>>
(转)Linxu磁盘体系知识介绍及磁盘介绍
查看>>
tkinter布局
查看>>
命令ord
查看>>
Sharepoint 2013搜索服务配置总结(实战)
查看>>
博客盈利请先考虑这七点
查看>>
使用 XMLBeans 进行编程
查看>>
写接口请求类型为get或post的时,参数定义的几种方式,如何用注解(原创)--雷锋...
查看>>
【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
查看>>
Java网络编程--socket服务器端与客户端讲解
查看>>