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. HBase(五): HBase运维管理

    HBase自带的很多工具可用于管理.分析.修复和调试,这些工具一部分的入口是hbase shell 客户端,另一部分是在hbase的Jar包中. 目录: hbck hfile 数据备份与恢复 Snap ...

  2. Bouncycastle中的RSA技术以及解决之道

    一个使用bouncycastle进行安全操作的实用类 2007-04-13 12:54 import java.io.*;import java.security.*;import java.secu ...

  3. 【linux】Cache和Buffer的区别

  4. hyper-v 中 安装 Centos 7.0 设置网络 教程

    安装环境是: 系统:win server 2012 r2 DataCenter hyper-v版本:6.3.9600.16384 centos版本:7.0 从网上下载的 centos 7.0  如果找 ...

  5. 基于.net开发chrome核心浏览器

    本文转载自:http://www.cnblogs.com/liulun/archive/2013/04/20/3031502.html 一: 上一篇的链接: 基于.net开发chrome核心浏览器[一 ...

  6. MySQL错误: could not retrieve transation read-only status server

    问题描述: java代码在开始事务后,先做了一个查询,再insert,此时会报:          java.sql.SQLException: could not retrieve transati ...

  7. bzoj3135: [Baltic2013]pipesd

    Description 有n个水库,m条管道.Jester会在某些管道中间凿开一个洞,让水流出来或者用水泵把水打进去.保证这个流速是偶数.对于一条管道(u, v),如果在中间凿开了一个洞让水流出来,流 ...

  8. Python编写一个Python脚本

    我想要一个可以为我的所有重要文件创建备份的程序.(下面测试环境为python2.7) 1.backup_ver1.py #!/usr/bin/python import os import time ...

  9. DateGridView中添加下拉框列并实现数据绑定、更改背景色

    1.添加下拉框 代码实现==> using System; using System.Collections.Generic; using System.Windows.Forms; names ...

  10. Python 模块学习:re模块

    今天学习了Python中有关正则表达式的知识.关于正则表达式的语法,不作过多解释,网上有许多学习的资料.这里主要介绍Python中常用的正则表达式处理函数. 方法/属性 作用 match() 决定 R ...