时间限制: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>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
int n,k;
int d[MAXN];
bool test(int T)
{
int cup=;
int hi_score=;
int ho_score=;
for(int i=;i<n;i++)
{
cup+=T;
if(cup<=d[i])
{
hi_score++;
cup=;
}
else
{
ho_score++;
cup-=d[i];
}
}
return ho_score>hi_score;
}
int main()
{
cin>>n>>k;
for(int i=;i<n;i++)
{
cin>>d[i];
}
int l=,r=INF;
while(r-l>)
{
int mid=(l+r)>>;
if(test(mid))
{
r=mid;
}
else
{
l=mid;
}
}
cout<<r<<endl;
return ;
}

最新文章

  1. C#后台调用公网接口(GET, POST)
  2. C#算法基础之递归排序
  3. 读书时间《JavaScript高级程序设计》七:表单
  4. AOP的成员介绍
  5. async简单使用
  6. Git版本控制的基本命令
  7. 扩展GDAL,支持CNSDTF格式(一)
  8. 课程设计个人报告——基于ARM实验箱的捕鱼游戏的设计与实现
  9. 爬虫基础之urllib库
  10. 从Java到JVM到OS线程睡眠
  11. go语言打造个人博客系统(一)
  12. asp.net core发布到linux
  13. python基础(16)-进程&amp;线程&amp;协程
  14. Telnet的三种登录方式
  15. Apple ID双重认证验证码无法输入问题
  16. 构建BSP (boardsupport packet)
  17. Sa身份登陆SQL SERVER失败的解决方案
  18. Codeforces Round #279 (Div. 2) 题解集合
  19. 【android】夜间模式简单实现
  20. 定义serialVersionUID的作用与意义整理

热门文章

  1. QT中文乱码处理
  2. Vue.js学习笔记 第八篇 组件
  3. Android电容屏(二):驱动调试分析【转】
  4. Python 条件判断语句(if ,elif, else)
  5. firefox和chrome实现页面打印自动分页
  6. 分布式技术 webservice
  7. C中的指针和字符串
  8. Python给数字前固定位数加零
  9. b树的实现(c++)
  10. 利用matplotlib中imshow()函数绘图