Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)
#1095 : HIHO Drinking Game
描述
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 题目分析:输入n个数,这n个掷的筛子数的大小范围是:[1, k].也就是大一行输入的两个数n,k;
现在要找到一个最小的T 让小ho赢了小hi。
规则如下:进行n轮,看谁的总分最高谁赢。
每次都会往杯子里加T的水,如果当前杯子里的水<=此次掷得筛子数小,hi分数++,小ho全喝光
如果当前杯子里的水<此次掷的筛子数,ho分数++,小ho喝掉对应此次筛子数量的水
最后谁的分高谁赢。
代码:#include <string.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <math.h>
#include <cctype>
#include <algorithm> using namespace std;
//枚举T
//如果d>=杯子里的水, hi++, ho全喝光
//如果 d<杯子里的水, ho++, ho喝掉d的量
//枚举区间[1, k+1]
int a[100001];
int n; bool Drink_t(int t) //测试看看每次t的水,小ho能不能赢?
{
int hi=0, ho=0;
int cnt=0; //代表杯子里的水
for(int i=0; i<n; i++)
{
cnt+=t;
if(a[i]>=cnt)
{
hi++;
cnt=0;
}
else if( a[i]<cnt )
{
ho++;
cnt=cnt-a[i];
}
}
if(hi>ho)
return false; //表示输了
else
return true; //表示赢了
} void B_search(int low, int high)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if( Drink_t(mid)==true && Drink_t(mid-1)==false ) //只有当前这个mid可以赢,一旦mid-1就不能赢了,这个答案才是最小的
{
break;
}
else if(Drink_t(mid)==true)
{
high=mid-1;
}
else if(Drink_t(mid)==false )
{
low=mid+1;
}
}
printf("%d\n", mid );
} int main()
{
int k;
while(scanf("%d %d", &n, &k)!=EOF )
{
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
B_search(1, k+1);
}
return 0;
}
Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)的更多相关文章
- hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)
#1103 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorit ...
- hihoCoder #1094 : Lost in the City(枚举,微软苏州校招笔试 12月27日 )
#1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He ...
- hihoCoder #1106 : Koch Snowflake 微软苏州校招笔试(1月17日)
描述 Koch Snowflake is one of the most famous factal. It is built by starting with an equilateral tria ...
- hihocoder #1094 : Lost in the City微软苏州校招笔试 12月27日 (建图不大【暴力枚举】 子图的4种形态 1Y )
#1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He ...
- 【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题
题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了. ...
- 【内推】2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐
2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐 大家好,目前微软Office365核心团队在美丽宜居的苏州有150多的社招职位虚位以待,欢迎大家自荐,推荐,转发!除以下列 ...
- 美团点评2017校招笔试真题-算法工程师A
美团点评2017校招笔试真题-算法工程师A 1.下面哪种STL容器的实现和其它三个不一样 A. set B. deque C. multimap D. map 正确答案: B STL的容器可以分为以下 ...
- 美团点评2017校招笔试真题-算法工程师B
美团点评2017校招笔试真题-算法工程师B 1.以下关于经典的k-means聚类的说法哪个是错误的? A:k-means聚类算法是全局收敛的 B:k-means的聚类结果和初始聚类中心点的选取有关 C ...
- hihoCoder#1095(二分搜索)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playing a drinking game called HI ...
随机推荐
- 具体一些的博弈论 sqrstone
Description 你有n个盒子用来放石头,每个盒子都有最大容量与初始的石头数, 两个人轮流放石头,每次必须选择一个盒子往里放数量不超过当前盒子中石头数的平方的石头 比如一个盒子当前有3个石头,你 ...
- javascript 对象初探 (六)--- call()和apply()初探
在javascript中,每个函数都具有call()和apply()两个方法,您可以用她们来触发函数,并指定相关的调用参数. 此外,这两个方法还有另一个功能,就是她可以让一个对象去‘借用‘另一个对象的 ...
- [反汇编练习] 160个CrackMe之031
[反汇编练习] 160个CrackMe之031. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- How to create a freehand tool
http://forums.esri.com/Thread.asp?c=159&f=1707&t=283694&mc=1 http://blog.sina.com.cn/s/b ...
- U-net图像分割
[Keras]基于SegNet和U-Net的遥感图像语义分割 2014 年,加州大学伯克利分校的 Long 等人提出全卷积网络(FCN),这使得卷积神经网络无需全连接层即可进行密集的像素预测,CNN ...
- linux下脚本监控网络流量
linux下脚本监控网络流量 学习了:https://blog.csdn.net/chenghuikai/article/details/48437479 学习了:http://www.jb51.ne ...
- hdu 4432 数学杂题
http://acm.hdu.edu.cn/showproblem.php?pid=4432 6分钟写的代码,一上午去调试,, 哎,一则题目没看懂就去写了,二则,哎,,恶心了.在坚持几天然后ACM退役 ...
- crtmp Server 开启rtsp服务功能
Crtmp Server 包含了rtsp 服务功能,如果需要一个简单轻量的rtsp服务,Crtmp Server会是不错的选择. 默认情况下,rtsp功能是关闭的,需要在配置文件中打开.window环 ...
- Python+Selenium框架-unittest执行脚本方法之addTest
本文开始介绍如何通过unittest来管理和执行测试用例,这一篇介绍unittest下addTest()方法来加载测试用例到测试套件中去.为了演示效果,我在前面文章的脚本基础上,新建了一个测试脚本,这 ...
- 2014年8月25日,收藏家和杀手——面向对象的C++和C(一)
近期事情特别多,睡眠也都非常晚,有点精神和身体混乱的感觉,所以想写写技术分析文章.让两者的我都调整一下.这篇技术分析文章是一直想写的,当前仅仅是开篇,有感觉的时候就写写,属于拼凑而成,兴许的篇章没有时 ...