Chocolate Eating(二分)

链接:https://ac.nowcoder.com/acm/problem/24724
来源:牛客网

题目描述
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_iH
i

(1 <= H_iH
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.
输入描述:

  • Line 1: Two space separated integers: N and D
  • Lines 2…N+1: Line i+1 contains a single integer: H_iH
    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
    输出
    复制
    24
    1
    1
    3
    4
    5

题意:略。

题记:简单二分。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=5e4+10;
int a[N],ans[N];
int n,d;

bool check(ll x){
    int k=1;
    ll sum=0;
    for(int i=1;i<=d-1;i++){
        sum/=2;
        while(k<=n&&sum<x){
            ans[k]=i;
            sum+=a[k++];
        }
        if(sum<x)return false;
    }
    sum/=2;
    while(k<=n){
        ans[k]=d;
        sum+=a[k++];
    }
    return sum>=x;
}

int main(){
    cin>>n>>d;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    ll l=1,r=1e11;
    while(l<r){
        ll mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    cout<<l<<endl;
    check(l);
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值