流水依依

希望朋友们有个好的身体,开开心心工作与学习。

博客园 首页 新随笔 联系 订阅 管理

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 ;
}

  

 

 

 

posted on 2013-10-07 12:49  流水依依  阅读(468)  评论(2编辑  收藏  举报