Fegla and the Bed Bugs

Fegla, also known as mmaw, is coaching a lot of teams. All these teams train together in one place,
unfortunately this place doesn’t have any good ventilation and is quite small relative to the number
of teams. All these circumstances resulted in a strange creature appearing! That creature is called
The Bed Bug!

These are parasitic bugs; they feed on human blood by biting them. What was strange and confused
Fegla, is that some of the team members did not get bitten at all! However, he was more interested in
eliminating these bugs. After observing the bugs’ behavior for some time, he concluded that he
needed to stop them from reproducing to eliminate them. They reproduce by getting very close to
each other.

And so, Fegla needs your help. Given a straight line of empty cells N and the number of bugs K, tell
Fegla the best assignment for the bugs to maximize the minimum number of empty cells between
each two consecutive bugs on that line.

For example, given N=4 and K=2, the answer would be 2, according to the best assignment:
Bed Bug Empty Empty Bed Bug

Input Specification
Input will start with an integer T representing the number of test cases. Followed by T lines each line
contains two integers N, K.

You can assume that

2 <= N <= 200
2 <= K <= N

Output Specification
For each test case in a separate line, output the minimum distance between EACH two consecutive
bugs in the best assignment.

Sample Input
2
4 2
3 2
Sample Output
2
1

思路:很典型的二分试题,因为答案满足单调性,且有judge函数满足贪心性质。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef unsigned long long LL ;
int N ,K ;
int judge(int Len){
int id=1 ;
for(int i=2;i<=K;i++)
id+=(Len+1) ;
return id<=N ;
}
int calc(){
int ans ,Left , Right ,Mid ;
Left=0 ;
Right=N ;
while(Left<=Right){
Mid=(Left+Right)>>1 ;
if(judge(Mid)){
ans=Mid ;
Left=Mid+1 ;
}
else
Right=Mid-1 ;
}
return ans ;
}
int main(){
int T ;
cin>>T ;
while(T--){
cin>>N>>K ;
cout<<calc()<<endl ;
}
return 0 ;
}

  

Fegla and the Bed Bugs 二分的更多相关文章

  1. Codeforces Gym 100500F Problem F. Door Lock 二分

    Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...

  2. codeforces 377B Preparing for the Contest 二分+优先队列

    题目链接 给你m个bug, 每个bug都有一个复杂度.n个人, 每个人有两个值, 一个是能力值, 当能力值>=bug的复杂度时才可以修复这个bug, 另一个是雇佣他需要的钱,掏一次钱就可以永久雇 ...

  3. CodeForces 377B---Preparing for the Contest(二分+贪心)

    C - Preparing for the Contest Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  4. (简单) POJ 2492 A Bug's Life,二分染色。

    Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...

  5. Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树

    B. Preparing for the Contest 题目连接: http://codeforces.com/contest/377/problem/B Description Soon ther ...

  6. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  7. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  8. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  9. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

随机推荐

  1. 史上最全Java表单验证封装类

    package com.tongrong.utils; import java.util.Collection; import java.util.Map; import java.util.rege ...

  2. JCaptcha做验证码遇到的问题引出的思考

    JCaptcha用来做用户登录时期的验证码的,但是今天将开放的应用系统部署到生产环境的时候,遇到了问题,总是提示验证码不对.后台报出来下面的错误: com.octo.captcha.service.C ...

  3. FileReader 基本介绍

    转自:http://blog.csdn.net/zk437092645/article/details/8745647 用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步 ...

  4. 【性能测试】性能测试总结<四>

    性能测试常见指标        性能测试说白了就是通过工具模拟多个用户对被测系统进行访问.然后查看系统对于多个用户发来请求的处理能力. 左边的两个小人表示两个用户,向右边服务器发送请求,然后得到服务器 ...

  5. EINTR、ERESTARTSYS和SIGINT

    1. 驱动使用down_interruptible,并在该函数返回非零值时返回-EINTR:应用程序不处理signal,使用CTRL-C退出应用程序: 驱动从down_interruptible返回, ...

  6. bzoj4561: [JLoi2016]圆的异或并

    Description 在平面直角坐标系中给定N个圆.已知这些圆两两没有交点,即两圆的关系只存在相离和包含.求这些圆的异或面 积并.异或面积并为:当一片区域在奇数个圆内则计算其面积,当一片区域在偶数个 ...

  7. Hadoop:使用原生python编写MapReduce

    功能实现 功能:统计文本文件中所有单词出现的频率功能. 下面是要统计的文本文件 [/root/hadooptest/input.txt] foo foo quux labs foo bar quux ...

  8. nokia5230 出厂设置

    你手机sim卡里的电话没事,还有储存卡里都没事,这个只是针对手机内存,如果不放心,拿你不用的手机卡,拔了内存卡格式化你好,我来具体说说吧首先如果你要单一恢复出厂设置,代码是输入*#7780#密码没改是 ...

  9. erlang和java通信

    连接在 https://guts.me/2014/07/27/erlang-communicates-with-java/ 代码在 https://github.com/mingshun/jinter ...

  10. c/c++多线程编程中最好不要加volatile

    来自https://www.zhihu.com/question/31459750 答主解释说:不能指望volatile能解决多线程竞争问题,除非所用的环境系统不可靠才会为了保险加上volatile, ...