P2985 [USACO10FEB]吃巧克力Chocolate Eating

本文解析了USACO中吃巧克力问题的算法实现,通过二分查找确定最优的吃巧克力策略,使得Bessie在D天内最不开心的一天也能达到最高的开心值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 P2985 [USACO10FEB]吃巧克力Chocolate Eating

题目描述

Bessie has received N (1 <= N <= 50,000) chocolates from the bulls, but doesn't want to eat them too quickly, so she wants to plan out her chocolate eating schedule for the next D (1 <= D <= 50,000) days in order to maximize her minimum happiness level over the set of those days.

Bessie's happiness level is an integer that starts at 0 and halves (rounding down if necessary) over night as she sleeps. However, when she eats chocolate i, her happiness level increases by integer H_i (1 <= H_i <= 1,000,000). If she eats chocolates on a day, her happiness for that day is considered the happiness level after she eats the chocolates. Bessie insists that she eat the chocolates in the order that she received them.

If more than one optimal solution exists, print any one of them.

Consider a sequence of 5 chocolates to be eaten over a period of 5 days; they respectively bring happiness (10, 40, 13, 22, 7).

If Bessie eats the first chocolate (10 happiness) on the first day and then waits to eat the others, her happiness level is 10 after the first day.

Here is the complete schedule which turns out to maximize her minimum happiness:

Day Wakeup happiness Happiness from eating Bedtime happiness 1 0 10+40 50

2 25 --- 25

3 12 13 25

4 12 22 34

5 17 7 24

The minimum bedtime happiness is 24, which turns out to be the best Bessie can do.

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

每块巧克力有一个开心值H_i (1 <= H_i <= 1,000,000),当某天你吃下那块巧克力时,你将获得那块巧克力的开心值。每一天的开心值是所有当天吃掉的巧克力的总开心值之和。每天晚上Bessie睡觉之后,它的开心值会减半。也就是说,比如昨天Bessie的开心值为50,那么今天早上我一醒来我就会有25点的开心值,舍去小数点后数字。另外,Bessie还有一个怪癖,她喜欢按照巧克力本来的排列顺序吃。

Bessie第一天的开心值为0,求一个每天吃巧克力的方案,使得Bessie最不开心的一天尽可能的开心。

输入输出格式

输入格式:

 

  • Line 1: Two space separated integers: N and D

  • Lines 2..N+1: Line i+1 contains a single integer: H_i

 

输出格式:

 

  • Line 1: A single integer, the highest Bessie's minimum happiness can be over the next D days

  • Lines 2..N+1: Line i+1 contains an integer that is the day on which Bessie eats chocolate i

 

输入输出样例

输入样例#1:
5 5 
10 
40 
13 
22 
7 
输出样例#1:
24 
1 
1 
3 
4 
5 
一道二分的题,坑点太多,一直过不了QAQ。
注意:1、居然忘记输出巧克力在那一天吃了,直接输出二分答案!!!2、开long long 就是开了9行开了吗!!!3、吃不完的最后一天吃,直接忽略orz!!!
 1 #include<cstdio>
 2 #include<cstring>
 3 long long c[50100];
 4 int v[50100];
 5 int n,m;
 6 bool check(long long x,bool flag)
 7 {
 8     int p = 0;
 9     long long now = 0;    //这里也要开long long !!! 
10     for (int i=1; i<=m; ++i)
11     {
12         now = now>>1;
13         while (now<x&&p<=n)
14         {
15             ++p;
16             if (flag) v[p] = i;
17             now += c[p];
18         }
19         if (now<x) return false ;
20     }
21     if (flag) 
22         for (int i=p+1; i<=n; ++i) //多余的都在最后一天吃 
23             v[i] = m; 
24     return true ;
25 }
26 int main()
27 {
28     long long ans = 0, l = 0, r = 0;
29     scanf("%d%d",&n,&m);
30     for (int i=1; i<=n; ++i)
31     {
32         scanf("%lld",&c[i]);
33         r += c[i];
34     }
35     while (l<=r)
36     {
37         long long mid = (l+r)>>1;
38         if (check(mid,0))
39         {
40             ans = mid;
41             l = mid+1;
42         }
43         else r = mid-1;
44     }
45     printf("%lld\n",ans);
46     check(ans,1);    //求出巧克力在那一天吃 
47     for (int i=1; i<=n; ++i)
48         printf("%d\n",v[i]);
49     return 0;
50 }

转载于:https://www.cnblogs.com/mjtcn/p/7096430.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值