#1095 : 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

题目分析:输入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 (微软苏州校招笔试)( *【二分搜索最优解】)的更多相关文章

  1. hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)

    #1103 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorit ...

  2. hihoCoder #1094 : Lost in the City(枚举,微软苏州校招笔试 12月27日 )

    #1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He ...

  3. hihoCoder #1106 : Koch Snowflake 微软苏州校招笔试(1月17日)

    描述 Koch Snowflake is one of the most famous factal. It is built by starting with an equilateral tria ...

  4. 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 ...

  5. 【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题

    题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了. ...

  6. 【内推】2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐

    2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐 大家好,目前微软Office365核心团队在美丽宜居的苏州有150多的社招职位虚位以待,欢迎大家自荐,推荐,转发!除以下列 ...

  7. 美团点评2017校招笔试真题-算法工程师A

    美团点评2017校招笔试真题-算法工程师A 1.下面哪种STL容器的实现和其它三个不一样 A. set B. deque C. multimap D. map 正确答案: B STL的容器可以分为以下 ...

  8. 美团点评2017校招笔试真题-算法工程师B

    美团点评2017校招笔试真题-算法工程师B 1.以下关于经典的k-means聚类的说法哪个是错误的? A:k-means聚类算法是全局收敛的 B:k-means的聚类结果和初始聚类中心点的选取有关 C ...

  9. hihoCoder#1095(二分搜索)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playing a drinking game called HI ...

随机推荐

  1. webstrom配置一键修复ESLint的报错

    因为项目本身有用eslint,而我这边没用,我这边提交上去别人update后就会提示很多eslint的格式错误提示,所以就在该项目里使用了eslint. 发现一般有两种安装方式,我使用的是webstr ...

  2. NEERC15

    2015-2016 ACM-ICPC Northeastern European Regional Contest 再开一个新坑吧 目前姿势有限,C.H.I仍然处于弃坑状态 代码戳这里 Problem ...

  3. 某考试 T3 sine

    推完一波式子之后发现是个矩阵23333. 其实只要发现是矩阵之后就是个水题了. #include<bits/stdc++.h> #define ll long long using nam ...

  4. Oracle PL/SQL块 多表查询(emp员工表、dept部门表、salgrade工资等级表)

    范例: 查询每个员工的编号,姓名,职位,工资,工资等级,部门名称 ●确定要使用的数据表 |- emp表:员工的编号.姓名.职位.工资 |- salgrade表:工资等级 |- dept表:部门名称 ● ...

  5. Maven依赖机制理解

    假设一个项目需要用到日志组件Log4j,那么有如下方式添加这个组件. 一.传统方式: 1.访问官网https://logging.apache.org/log4j/2.x/download.html, ...

  6. SQL-基础学习2--ORDER BY ,DESC,WHERE, BETWEEN,AND ,OR ,IN ,NOT

    所使用的数据库资料在:数据库资料   第三课:排序检索数据 3.1 排序数据 按单列排序 如果不排序,数据一般将以它在底层表中出现的顺序显示,这有可能是数据最初添加到表中的顺序.但是,如果数据随后进行 ...

  7. 主流浏览器js 引擎内核市场份额attialx总结vOa9

    原文: http://blog.csdn.net/attilax/article/details/40016... 时间: 2014-10-12 atitit.. 主流浏览器 js 引擎 内核 市场份 ...

  8. java设计模式之-建造者模式

    建造者模式可以将复杂的构建与其表示相分离,是的相同的构建过程可以创建出不同的表示.     建造者模式与抽象工厂的差别是:在建造者模式里,有个指导者,这个指导者来管理建造者.用户与指导者相互联系,指导 ...

  9. python(8)- python基础数据类型

    数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视 频.网页等各种各样的数据,不同的数据,需要 ...

  10. XSS过滤

    XSS过滤封装用法 封装到app01/form.py文件中进行验证 from django.forms import Form,widgets,fields class ArticleForm(For ...