C. Party Lemonade

链接

[http://codeforces.com/group/1EzrFFyOc0/contest/913/problem/C]

分析

看代码,巧妙的贪心

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll n,l;
	ll c[40];
	int i;
	//freopen("in.txt","r",stdin);
	while(cin>>n>>l){
		ll ans=0;
		for(i=0;i<n;i++)
			cin>>c[i];
		
		for(i=1;i<n;i++)
			if(2*c[i-1]<c[i]) c[i]=2*c[i-1];//选择性价比最高 
	//下面很关键,你可以把一个十进制数换成二进制数,因为题目刚好type i has volume 2^i-1 liters 
	  for(i=0;i<n;i++){
	  	if(ans>c[i]) ans=c[i];//如果已经买的价格超过了现在的价格而现在的容量一定大于已经买的
		  //你多买了而且价格还底何乐而不为呢 
	  	if(l&1) ans+=c[i];//当二进制位为1的时候就买 
	  	l/=2;//相当于十进制化为二进制的过程 
	  }
	  l*=2;//因为上面只枚举到第n种类型,可能没买完要买的而且我们每个for都除2所以要乘以2 
	  ans+=l*c[n-1];//选最后的类型,因为剩下的是l*2^n-1升 
	  cout<<ans<<endl;
	}
	return 0;
}
posted @ 2018-10-05 12:30  ChunhaoMo  阅读(215)  评论(1编辑  收藏  举报