Chemistry
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的更多相关文章
- CF 445B DZY Loves Chemistry(并查集)
题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second memory limit per test:256 megabytes D ...
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...
- 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 ...
- CodeForces 445B DZY Loves Chemistry
DZY Loves Chemistry Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- 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/ ...
- HDU-5347 MZL's chemistry
http://acm.hdu.edu.cn/showproblem.php?pid=5347 MZL's chemistry Time Limit: 2000/1000 MS (Java/Others ...
- Codeforces Round #312 (Div. 2) C.Amr and Chemistry
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...
- cf445B DZY Loves Chemistry
B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...
- hdu5347 MZL's chemistry(打表)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud MZL's chemistry Time Limit: 2000/1000 MS ...
- hdu 5625 Clarke and chemistry
Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke turned i ...
随机推荐
- 使用Lingo增强JMS
虽然activemq+jencks的jms轻量级解决方案已经很好地在psa中work了,尤其spring的JmsTemplate使得代码更简单,但是还是存在问题. 问题来自暑期做psa的时候,link ...
- 如何使用HTTP压缩优化服务器
鉴于互联网上的宽带有限,网络管理人员任何旨在加速接入速度的努力都是有价值的.其中的一个方法就是通过HTTP压缩技术实现接入速度的加速,它通过减少在服务器和客户端之间传输的数据量,显著地提高网站的性能. ...
- python的猴子补丁monkey patch
monkey patch指的是在运行时动态替换,一般是在startup的时候. 用过gevent就会知道,会在最开头的地方gevent.monkey.patch_all();把标准库中的thread/ ...
- 深入java虚拟机(一) 虚拟机内存结构
java虚拟机所管理的内存区域(运行时数据区)主要分为如下几个部分:堆(heap).方法区(method area).虚拟机栈(VM stack).本地方法栈(native method stack) ...
- 原生JS返回顶部,带返回效果
有些网站当滑到一定高度时右下角会有一个按钮,你只要一点就可以直接返回顶部了.那这个功能是怎么做到的呢.其实不算太难: 首先我们先在网页中创建一个按钮,上面写上返回顶部,把它的样式改成固定定位,之后想要 ...
- iOS与导航相关的都在这
// 设置导航背景图片 (一旦设置背景图片(变为不透明),透明层不起作用) [self.navigationBar setBackgroundImage:[UIImage imageNamed:@&q ...
- DOM 编程入门
DOM (Document Object Model) 文档对象模型 文档: 标记型文档(具有标签, 属性以及标签中封装的数据) 对象: 封装了属性和行为的实例, 可以被直接调用 模型: 所有标记型文 ...
- 剑指Offer——斐波那契数列
题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39 分析: 递归解法肯定相当耗时. 因为当n=4时,程序是这样子递归运算的:Fibonacci( ...
- 报错:Cannot remove entries from nonexistent file c:\program files\anaconda3\lib\site-packages\easy-install.pth
Outline 这两天通过“掘金量化终端”跑模型策略,之前装好环境一直ok,可以顺畅的Running~ 下午重装了下 Anaconda,刚才跑的时候提示 缺少“gm”模块 (掘金量化必须包): 就按照 ...
- 同一TextView上内容的不同显示(最新)-SpannableString
上次发了一篇同一TextView内容的不同显示这篇文章. 有关颜色的不同显示,我使用了最简单可是也最复杂的方法.忘记使用SpannableString,现又一次发一下,大家參考下. TextView组 ...