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. 查看SQL执行计划

    一用户进入某界面慢得要死,查看SQL执行计划如下(具体SQL语句就不完全公布了,截断的如下): call     count       cpu    elapsed       disk       ...

  2. 【原创】Quartz代码详解

    阅读目录 简单介绍 章节1:Quartz简单实例 章节2:Job.JobDetail.JobBuilder 章节3:Trigger.TriggerBuilder 章节4:Scheduler 章节5:J ...

  3. xmodem, ymodem & zmodem

    这三个是modem文件传输协议. https://en.wikipedia.org/wiki/XMODEM https://en.wikipedia.org/wiki/YMODEM https://e ...

  4. IntelliJ IDEA设置自动导入包

    IntelliJ IDEA可以自动优化导入包,但是有多个同名的类位于不同的包时,需要自己手动使用Alt + Enter进行导入. Settings→Editor→General→Auto Import ...

  5. Python列表,元组,字典,序列,引用

    1.列表 # Filename: using_list.py # This is my shopping list shoplist=["apple", "mango&q ...

  6. JavaScript-日期格式化(一)

    /** * 日期格式化, * @param date 要格式化的日期 * @param pattern 进行格式化的模式 * 支持的模式字母有: * y:年, * M:年中的月份(1-12), * d ...

  7. PLSQL_性能优化系列03_Oracle Parallel并发处理

    2014-09-25 Created By BaoXinjian

  8. POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题

    在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以 ...

  9. Python分布式爬虫原理

    转载 permike 原文 Python分布式爬虫原理 首先,我们先来看看,如果是人正常的行为,是如何获取网页内容的. (1)打开浏览器,输入URL,打开源网页 (2)选取我们想要的内容,包括标题,作 ...

  10. SQL数据插入:将截断字符串或二进制数据

    将图片等较多数据保存到数据库的过程中会遇到: -- :: EL图片保存到数据库出错 条码:DE1132 异常原因:System.Data.SqlClient.SqlException: 将截断字符串或 ...