描述
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
题意:
两个人A,B玩游戏,一共N轮,每一轮A会往B的杯子里倒入T毫升水,然后B掷一个K面的骰子范围为1到K,如果掷出的点数小于等于d毫升,那么A加一分,B把杯子里剩下的水喝掉,否则A喝掉杯子中的d毫升水,并且B加一分。最后得分高的胜出。现在假设B能预知每次掷骰子的点数,求最小的T使得B一定获胜。
思路:
二分求T啦,每次判断mid是否满足条件。直接上框架就好。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e5+5;
int n,k;
int A[maxn];
bool judge(int x)
{
int Hi = 0, Ho = 0;
long long remain = 0;
for (int i = 0; i < n; ++i) {
remain += x;
if (remain <= A[i]) {
++Hi;
remain = 0;
}
else {
++Ho;
remain -= A[i];
}
}
if (Hi < Ho) return true;
else
return false;
}
int Search(int low,int high)
{
while(low < high) {
int mid = (low + high) / 2;
if (!judge(mid)) low = mid + 1;
else
high = mid ;
}
return low;
}
int main()
{
cin >> n >> k;
for (int i = 0; i < n; ++i)
cin >> A[i];
int res = Search(1,1e9);
cout << res << endl;
return 0;
}