HIHO Drinking Game

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Little Hi and Little Ho are playing a drinking game called HIHO. The game comprises N rounds. Each round, Little Hi pours T milliliter of water into Little Ho’s cup then Little Ho rolls a K-faces dice to get a random number d among 1 to K. If the remaining water in Little Ho’s cup is less than or equal to d milliliter Little Hi gets one score and Little Ho drinks up the remaining water, otherwise Little Ho gets one score and Little Ho drinks exactly d milliliter of water from his cup. After N rounds who has the most scores wins.

Here comes the problem. If Little Ho can predict the number d of N rounds in the game what is the minimum value of T that makes Little Ho the winner? You may assume that no matter how much water is added, Little Ho’s cup would never be full.

输入
The first line contains N(1 <= N <= 100000, N is odd) and K(1 <= K <= 100000).
The second line contains N numbers, Little Ho’s predicted number d of N rounds.

输出
Output the minimum value of T that makes Little Ho the winner.

样例输入
5 6
3 6 6 2 1
样例输出
4

#include "iostream"
#include "fstream"
#include "math.h"
using namespace std;

int N, K;
int d[100000];  //每次摇出的点数

int f(int T)
{
    int score = 0;  //小Ho的得分
    int rest = 0;   //杯中剩余的饮料
    for(int i=0; i<N; i++)
    {
        rest += T;        //小Hi向杯子中倒入T单位饮料
        if(d[i] < rest)   // 若杯子中饮料大于第i轮的d
        {
            score++;      // 小Ho获得一分
            rest -= d[i]; // 小Ho喝掉d个单位饮料
        }
        else
            rest = 0;     // 小Ho喝掉全部的饮料
    }
    return score;
}

int main()
{
    //ifstream cin;
    //cin.open("1.txt");
    cin >> N >> K;
    int i;
    for(i=0; i<N; i++)
        cin >> d[i];

    int left, right;
    left = 0;
    right = K + 1;
    while(left+1 < right)
    {
        int mid = (left + right) >> 1;
        if(f(mid) < ((N+1)/2))
            left = mid;
        else
            right = mid;
    }
    cout << right;   //f(left) < (N+1)/2  f(right) >= (N+1)/2
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值