Problem A. Chemistry

Input file: chemistry.in

Output file: chemistry.out

Time limit: 1 seconds

Memory limit: 256 megabytes

香香手中有n瓶化学药剂,每瓶化学药剂都有一个能量值ai。现在她想把这些药剂的能量值全部变成一样的,她有两台机器,其中一台机器一次可以把一瓶药剂的能量值翻倍,ai变成2ai,另一台机器一次可以把一瓶药剂的能量值变为一半,ai变成。

然而每台每次启动都需要单位1的能量,她所储存的能量不多了,请你帮她计算一下用最少的能量完成这个任务。

Input

第一行输入一个n(1 <= n <= 10^5 )

第二行输入n个数,用空格隔开,表示每瓶药剂的能量值ai

(1 <= ai <= 10^5)

Output

输出一行,表示最少需要的能量值是多少。

Sample test(s)

input

3
4 8 2

output

2

input

3
3 5 6

output

5

样例解释:

第一个样例能量值全部为成4 ,第二个样例全部变为1。

范围:

对于10% , n = 2

对于30% , n <= 100

对于另10% , ai <= 1000

对于100% , n <= 10^5 , ai <= 10^5

  分析:

  明显的DP,f[i][j]表示前i个数全部变成j所要耗费的能量,然后就是各种优化。。。。。

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int MAX=;
const int inf=0x3f3f3f3f;
int a[MAX];
int b[MAX],b2[MAX];
int c[];
int f[][];
int may[];
int N;
int ANS=inf;
int MIN;
int main(){
// freopen("chemistry.in","r",stdin);
// freopen("chemistry.out","w",stdout);
scanf("%d",&N);
for(int i=;i<=N;i++) scanf("%d",&a[i]);
sort(a+,a+N+);
for(int i=;i<=N;i++) b[i]=a[i],b2[i]=a[i]; for(int i=;i<=N;i++){//筛出来可能存在的数字
may[b[i]]++; while(b[i]*<=*a[N]){
may[b[i]*]++;
b[i]*=;
}
while(b2[i]/>=){
may[b2[i]/]++;
b2[i]/=;
} } int now=;
for(int i=;i<=*a[N];i++){
if(may[i]==N){//不为偶数的
now++;
c[now]=i;
}
} for(int k=;k<=*a[N]; ){
if(may[k]!=N){
now++;
c[now]=k;
}
k<<=;
}
//f[i][j]表示前i个数,全部变成c[j]时,所需的能量 for(int j=;j<=now;j++){
for(int i=;i<=N;i++){//DP
int num=c[j];
int ans=; if(may[num]==N){//序列中的每个数都可以直升直降得到 if(num==a[i]) ans=;
if(num>a[i]){
int x=a[i];
while(x!=num){
x*=;
ans++;
}
}
if(num<a[i]){
int x=a[i];
while(x!=num){
x/=;
ans++;
}
}
}
//*********
else{//有的数字必须先降再升可以达到
int x=a[i];
if(num==a[i]) ans=;
if(num>a[i]){
while(x!=num){
x<<=;
ans++;
if(x>num){
ans=inf;
break;
}
}
}
if(ans==&&num!=a[i])
ans=inf;
if(x!=num){
int ans1=;
int x=a[i];
while(x!=){
if(x==num) break;
else{
x>>=;
ans1++;
}
}
while(x!=num){
x<<=;
ans1++;
}
ans=min(ans,ans1);
}
}
f[][j]=f[][j]+ans;
f[][j]=f[][j];
f[][j]=;
if(i==N)
ANS=min(ANS,f[][j]);
}
} cout<<ANS;
return ;
}

Chemistry的更多相关文章

  1. CF 445B DZY Loves Chemistry(并查集)

    题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second     memory limit per test:256 megabytes D ...

  2. 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry

    C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...

  3. DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏

    DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. CodeForces 445B DZY Loves Chemistry

    DZY Loves Chemistry Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  5. 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/ ...

  6. HDU-5347 MZL's chemistry

    http://acm.hdu.edu.cn/showproblem.php?pid=5347 MZL's chemistry Time Limit: 2000/1000 MS (Java/Others ...

  7. Codeforces Round #312 (Div. 2) C.Amr and Chemistry

    Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...

  8. cf445B DZY Loves Chemistry

    B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. hdu5347 MZL's chemistry(打表)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's chemistry Time Limit: 2000/1000 MS ...

  10. hdu 5625 Clarke and chemistry

    Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke turned i ...

随机推荐

  1. Visual Studio 2015 自定义文件编译

    自己编译好了 QT 5.6.0 Alpha 版本后,如何使用 VS2015 创建 QT 工程呢? 1.安装插件,安装失败!因为 VS2015 不再支持 ADDIN , 所以 QT-VS-ADDIN 只 ...

  2. Redis 连接失败redis Can't init enough connections amount!

    Can't init enough connections amount! Only 0 from 10 were initialized. Server: IP:6379 无法初始化足够的连接数量! ...

  3. 【BZOJ2962】序列操作 线段树

    [BZOJ2962]序列操作 Description 有一个长度为n的序列,有三个操作1.I a b c表示将[a,b]这一段区间的元素集体增加c,2.R a b表示将[a,b]区间内所有元素变成相反 ...

  4. 【BZOJ3673/3674】可持久化并查集/可持久化并查集加强版 可持久化线段树

    [BZOJ3674]可持久化并查集加强版 Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了! ...

  5. Android 界面滑动卡顿分析与解决方案(入门)

    Android 界面滑动卡顿分析与解决方案(入门) 导致Android界面滑动卡顿主要有两个原因: 1.UI线程(main)有耗时操作 2.视图渲染时间过长,导致卡顿 目前只讲第1点,第二点相对比较复 ...

  6. ASP.Net请求处理机制初步探索之旅 - Part 3 管道(转)

    开篇:上一篇我们了解了一个ASP.Net页面请求的核心处理入口,它经历了三个重要的入口,分别是:ISAPIRuntime.ProcessRequest().HttpRuntime.ProcessReq ...

  7. mysql中给表添加字段

    添加字段: 格式:alter table 表名 add 字段名 字段类型 ; 如:给表stu_info 添加一个字段type,类型为varchar(30) alter table stu_info a ...

  8. 修改/etc/hosts 云服务器 没有做外网转内网的优化

    [root@a mapReduceLog]# scp /data/visitlog/* root@d:/data/mapReduceVisitorLog/a/root@d's password:vis ...

  9. SOE 中调用第三方dll

    一.简介 在利用soe实现server的扩展的时候,有些时候,需要调用第三方的dll库.官网中给出了明确的说明,soe中是可以添加第三方的dll文件,但是一直没有测试.按照官方的步骤应该是一个非常的简 ...

  10. 小程序wxParse插件的使用

    微信小程序输出html内容数据插件wxParse,可以把带html标签的数据输出为微信小程序正常显示的格式,wxParse插件带有演示,也有使用文档说明. 下载地址:https://github.co ...