题目1 : HIHO Drinking Game
-
5 6 3 6 6 2 1
样例输出
-
4
描述
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.
题意分析
小Hi和小Ho正在玩这样一个游戏,在每局游戏的开始,小Hi手持一瓶可以认为是无穷无尽的饮料,而小Ho手中有一个空杯子。
一局游戏分为N轮,在每轮行动中,小Hi先向小Ho手中的杯子倒入T个单位的饮料(倒入的数量在一局游戏开始之前约定好且在整局游戏中固定),然后小Ho掷出一个均匀的K面骰子得到一个1..K之间的数d,如果杯中饮料的单位数小于等于d,则小Hi记一分,且小Ho将杯中剩余饮料一饮而尽,否则小Ho记一分,小Ho喝掉杯中d个单位的饮料。在N轮结束后,分高者获胜。
那么问题来了,如果小Ho能够预测这局中每轮自己所掷出的点数,那么最小的能使得小Ho获胜的T(每轮小Hi倒入小Ho杯子的饮料的单位数)是多少?
杯子的饮料的单位数和你选着的T有关。
应为T越大Ho的分数就越大,所以可以用二分
中间题意没看懂,一直是WA。
#include
using namespace std;
const int N = 100001;
int a[N];
int n, m;
int check(int k)
{
int cnt = 0;
int res = 0;
for(int i = 0; i < n; i++)
{
res += k;
if(res > a[i])
{
cnt++;
res -= a[i];
}
else
res = 0;
}
return cnt;
}
int main(void)
{
while(cin >> n >> m)
{
int i, j;
for(i = 0; i < n; i++) cin >> a[i];
i = 0;
j = m+1;
while(i < j)
{
int mid = (i+j)/2;
if(check(mid) > n/2)
{
j = mid-1;
}
else
{
i = mid+1;
}
}
if(check(i) <= n/2) i++;
cout << i << endl;
}
return 0;
}