Fegla and the Bed Bugs 二分
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 二分的更多相关文章
- Codeforces Gym 100500F Problem F. Door Lock 二分
Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...
- codeforces 377B Preparing for the Contest 二分+优先队列
题目链接 给你m个bug, 每个bug都有一个复杂度.n个人, 每个人有两个值, 一个是能力值, 当能力值>=bug的复杂度时才可以修复这个bug, 另一个是雇佣他需要的钱,掏一次钱就可以永久雇 ...
- CodeForces 377B---Preparing for the Contest(二分+贪心)
C - Preparing for the Contest Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- (简单) POJ 2492 A Bug's Life,二分染色。
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
- 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 ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]
2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 3352 Solved: 919[Submit][Stat ...
- 整体二分QAQ
POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...
- Common Bugs in C Programming
There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...
随机推荐
- Message,MessageQueue,Looper,Handler详解+实例
Message,MessageQueue,Looper,Handler详解+实例 原文地址 Android的Handler使用(这篇简单介绍Handler的使用) 一.几个关键概念 1.Message ...
- session_write_close()
功能: 结束当前的session 操作 保存session 数据 说的很明白了, 当脚本请求没有调用session_write_close(); 时虽然 session 的数据是存储住了.但是 s ...
- git tag知多少
这个命令,其实很有用,类似clearcase中的label,就是给一个版本设置一个标记(标签),方便后期查找特定的版本. tag和commit的sha1那串字符串的关系,不是很大,但是还是要说一下的. ...
- [转]session缓存机制和三种对象状态
摘自 http://blog.csdn.net/csh624366188/article/details/7612142 Hibernate 的Session就是其中的一个,它提供了基本的增,删,改, ...
- Object-C中需要注意的小细节
--------------------------------------------关于命名------------------------------------------------- 1. ...
- centos 安装sphinx
官网下载 :http://sphinxsearch.com/downloads/sphinx-2.2.10-release.tar.gz/thankyou.html 安装sphinx 解压 tar z ...
- JDBC的URL设置allowMultiQueries的原因
如下的一个普通JDBC示例: String user ="root";String password = "root";String url = "j ...
- makefile学习笔记(多目录嵌套调用、变量使用)
http://blog.csdn.net/leexiang_han/article/details/9274229 学习了几天的makefile的嵌套调用编写也有一些心得,先声明,我也是初学者写文 ...
- 204. Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. ============= 找质数 ...
- eclipse下编译hadoop源代码(转)
hadoop是一个分布式存储和分布式计算的框架.在日常使用hadoop时,我们会发现hadoop不能完全满足我们的需要,我们可能需要修改hadoop的源代码并重新编译.打包. 下面将详细描述如何从sv ...