题目链接

Solution

先直接二分答案,然后贪心判断,一旦少于答案就吃一块。

思路很简单,有一点细节。

  • 一天内可以不吃巧克力.
  • 注意处理最后时没吃完的全部在最后一天吃完.

Code

  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define N 50008
  4. #define inf 0x3f3f3f3f3f3f3f
  5. using namespace std;
  6. void in(ll &x)
  7. {
  8. char ch=getchar();ll f=1,w=0;
  9. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  10. while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
  11. x=f*w; return;
  12. }
  13. ll n,d,a[N],bl[N];
  14. bool judge(ll st)
  15. {
  16. ll now=0,res=d;
  17. for(int i=1;i<=n;i++)
  18. {
  19. if(now<st)
  20. {bl[i]=d-res+1;now+=a[i];}
  21. else {
  22. while(1){
  23. now=now/2;
  24. if(now<st)break;
  25. res--;
  26. }
  27. res--; now+=a[i];
  28. bl[i]=d-res+1;
  29. }
  30. }
  31. while(1)
  32. {
  33. if(now<=st)break;
  34. res--; now=now/2;
  35. }
  36. if(res<1)return 1;
  37. if(res==1&&now>=st)return 1;
  38. return 0;
  39. }
  40. int main()
  41. {
  42. in(n),in(d);
  43. for(int i=1;i<=n;i++) in(a[i]);
  44. ll l=0,r=inf;
  45. while(l<=r)
  46. {
  47. ll mid=(l+r)/2;
  48. if(judge(mid))l=mid+1;
  49. else r=mid-1;
  50. }
  51. cout<<l-1<<endl;
  52. judge(l-1);
  53. for(int i=1;i<=n;i++)
  54. {
  55. if(bl[i]==0||bl[i]>d)bl[i]=d;
  56. printf("%lld\n",bl[i]);
  57. }
  58. return 0;
  59. }

[USACO10FEB] 吃巧克力Chocolate Eating (二分答案)的更多相关文章

  1. P2985 [USACO10FEB]吃巧克力Chocolate Eating

    P2985 [USACO10FEB]吃巧克力Chocolate Eating 题目描述 Bessie has received N (1 <= N <= 50,000) chocolate ...

  2. [USACO10FEB]吃巧克力Chocolate Eating

    题目:洛谷P2985. 题目大意:有n块巧克力要吃d天,并且只能按顺序吃.一块巧克力有一个开心值,吃了就能增加开心值.一个人初始开心值为0,且每天早上开心值变为原来的一半.问如何吃巧克力才能使开心值最 ...

  3. luogu P2985 [USACO10FEB]吃巧克力Chocolate Eating

    题目描述 Bessie拿到了N (1 <= N <= 50,000)块巧克力.她决定想个办法吃掉这些巧克力,使得它在吃巧克力的这段时间里,最不开心的一天尽可能的开心.并且一共吃D (1 & ...

  4. BZOJ 2016: [Usaco2010]Chocolate Eating( 二分答案 )

    因为没注意到long long 就 TLE 了... 二分一下答案就Ok了.. ------------------------------------------------------------ ...

  5. 洛谷——P2983 [USACO10FEB]购买巧克力Chocolate Buying

    P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...

  6. 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying 题解

    P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...

  7. 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying

    购买巧克力Chocolate Buying 乍一看以为是背包,然后交了一个感觉没错的背包上去. #include <iostream> #include <cstdio> #i ...

  8. P2983 [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  9. 【洛谷】P2983 [USACO10FEB]购买巧克力Chocolate Buying(贪心)

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

随机推荐

  1. jmeter测试https请求

    测试https请求时,需要添加证书 目录 1.下载证书 2.导入 3.执行https请求 1.下载证书 在浏览器中打开要测试的https协议的网站,以谷歌为例打开,下载证书到桌面 4.一直点击下一步 ...

  2. JSP 定义行列数表单创建表格

    1.添加行数 .列数提交表单 <!doctype html> <html> <head> <title>setTable-发送表单</title& ...

  3. python实现读取配置文件

    实现代码如下: # 读取配置文件,取其值组成列表class ReadTxt: def read_txt(self,path): li_info = [] with open(path) as f: l ...

  4. GARENA笔试sql20190926

    create database garena; use garena; create table players( account_id int, name varchar(20), country ...

  5. 获取jQuery DataTables 的checked选中行

    $(function () { var  tabel = $('#userlist').DataTable({        destroy: true, //Cannot reinitialise ...

  6. 23.协程的使用场景,高I/O密集型程序

    import time def one_hundred_millionA(): start_time = time.time() i = 0 for _ in range(100000000): i ...

  7. forEach究竟能不能改变数组的值

    forEach究竟能不能改变数组的值 :https://blog.csdn.net/ZhengKehang/article/details/81281563 初学者每次提到Array对象的时候有些烦人 ...

  8. C# DATETIME格式转换汇总 根据日期获取星期

    原文:C# DATETIME格式转换汇总 根据日期获取星期 C# DateTime.Now.Year --2019(年) DateTime.Now.Month --9(月) DateTime.Now. ...

  9. 【问题解决方案】Linux中进入目录下文件夹

    win系统中直接 cd+空格+文件夹名 Linux下 cd+空格+./+文件名 其中句点表示"当前目录" 除非在根目录不加,或者把路径写全用绝对路径进入 Linux下切换路径的相关 ...

  10. UIViewController push或presentViewController 弹出方式

    //导航控制器数量 add xjz 判断是push还是present出来的 NSArray *viewcontrollers = self.navigationController.viewContr ...