bzoj2016[Usaco2010]Chocolate Eating*

bzoj2016[Usaco2010]Chocolate Eating

题意:

n块巧克力,每次吃可以增加ai点快乐,每天早晨睡觉起来快乐值会减半,求如何使d天睡觉前的最小快乐值最大。n,d≤50000

题解:

二分快乐值,每天不够就吃。注意如果最后一天有剩余巧克力,必须将其全部吃完。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define ll long long
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 #define maxn 100010
 7 using namespace std;
 8 
 9 inline ll read(){
10     char ch=getchar(); ll f=1,x=0;
11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
12     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
13     return f*x;
14 }
15 int n,d,bel[maxn]; ll h[maxn],ans1,ans2[maxn];
16 bool check(ll x){
17     ll hap=0; int p=1;
18     inc(i,1,d){
19         while(hap<x&&p<=n)hap+=h[p],bel[p]=i,p++; if(hap<x)return 0; hap>>=1;
20     }
21     while(p<=n)bel[p]=d,p++; inc(i,1,n)ans2[i]=bel[i]; ans1=x; return 1;
22 }
23 int main(){
24     n=read(); d=read(); inc(i,1,n)h[i]=read();
25     ll l=1,r=50000000000LL;
26     while(l<=r){
27         ll mid=(l+r)>>1; if(check(mid))l=mid+1;else r=mid-1;
28     }
29     printf("%lld\n",ans1); inc(i,1,n)printf("%lld\n",ans2[i]);
30 }

 

20160811

posted @ 2016-08-15 07:42  YuanZiming  阅读(176)  评论(0编辑  收藏  举报