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 ...
随机推荐
- jquery.find()
http://www.365mini.com/page/jquery-find.htm
- Eclipse换常用的快捷键
还是喜欢ctrl+tab键来切换窗口,ctrl+f6实在不好使. 修改方法:在eclipse中Window -> Perferences -> General -> Keys -&g ...
- Android官方提供的支持不同屏幕大小的全部方法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8830286 原文地址为:http://developer.android.com/ ...
- android学习笔记24——事件处理
事件处理 android提供了两种事件处理机制: 1.基于回调的事件处理 2.基于监听器的事件处理(通过绑定特定事件监听器) 注意: android对于基于回调的事件处理而言,主要做法就是重写andr ...
- SQL Server附加数据库文件出错
场景: 新装一台数据库服务器,装好后,附加数据库时出错.附加前的数据库架构没有在新服务器上安装.新服务器上只有默认dbo架构. 解决: 以windows身份验证登录,附加正常. 错误码可能为5120. ...
- item3 二维数组中的查找[剑指offer]
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有这个整数? 8 9 思路:查找7 ...
- angularjs中ng-route和ui-router简单用法的代码比较
1.使用ng-route: app.js中的写法: var app=angular.module('birthdayApp',['ngRoute']); app.config(function($ro ...
- JavaScript 设置、读取Cookie
1.设置Cookie //设置cookie function setCookie(cookieName, cookieValue, cookieExpires, cookiePath) { cooki ...
- 使用SparkSQL实现多线程分页查询并写入文件
一.由于具有多张宽表且字段较多,每个宽表数据大概为4000万条,根据业务逻辑拼接别名,并每张宽表的固定字段进行left join 拼接SQL.这样就能根据每个宽表的主列,根据每个宽表的不同字段关联出一 ...
- DBA_Oracle DBA常用SQL汇总(概念)
2014-06-20 Created By BaoXinjian