首页 > 代码库 > BZOJ 2016: [Usaco2010]Chocolate Eating

BZOJ 2016: [Usaco2010]Chocolate Eating

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2016

解:

 典型的二分,然而一开始我只写了20分。。。

1.要开long long.

2.到了最后如果巧克力还有的剩的话,要在最后一天把它们都吃掉

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,d,h[50010],belong[50010],belong2[50010];
long long ans;
bool check(long long x)
{
  long long now=0;
  int k=1;
  for (int i=1;i<=d;i++)
  {
      now=now/2;
      while (now<x)
      {
        if (k>n) return false;
      now+=h[k];
      belong2[k]=i;
      k++; 
    }
  }
  while (k<=n)
  {
    belong2[k]=d;
    k++;    
  } 
  for (int i=1;i<=n;i++) belong[i]=belong2[i];
  return true;
}
int main()
{
  long long r=0;
  scanf("%d%d",&n,&d);
  for (int i=1;i<=n;i++) 
  {
    scanf("%d",&h[i]);
    r+=h[i];
  }
  long long l=0;
  while (l<=r)
  {
    long long mid=(l+r)/2;
    if (check(mid))
    {
      ans=mid;
      l=mid+1;
    }
    else r=mid-1;
  }
  printf("%lld\n",ans);
  for (int i=1;i<=n;i++) printf("%d\n",belong[i]);
  return 0;
}

 

BZOJ 2016: [Usaco2010]Chocolate Eating