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

  

 

 

 

转载于:https://www.cnblogs.com/liyangtianmen/p/3355283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值