zoukankan      html  css  js  c++  java
  • 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 ;
    }
    

      

  • 相关阅读:
    「运维之美」技术周刊 ( 第 2 期 )
    Win10下MySQL(5.7和8两个版本)忘记root密码的解决办法
    SpringBoot使用自定义注解实现简单参数加密解密(注解+HandlerMethodArgumentResolver)
    Set的交集、差集踩坑记录
    RocketMQ控制台命令
    IDEA远程连接和上传文件到服务器
    记录一下JProfiler的使用
    点击验证码刷新的实现
    算法学习:我终于明白二分查找的时间复杂度为什么是O(logn)了
    Ubuntu:一个部署好的tomcat应用(war包)怎么用Nginx实现动静分离?
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3355283.html
Copyright © 2011-2022 走看看