zoukankan      html  css  js  c++  java
  • codeforces 655C C. Enduring Exodus(二分)

    题目链接:

    C. Enduring Exodus

     

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his k cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of nrooms located in a row, some of which are occupied.

    Farmer John wants to book a set of k + 1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms i and j is defined as |j - i|. Help Farmer John protect his cows by calculating this minimum possible distance.

    Input

    The first line of the input contains two integers n and k (1 ≤ k < n ≤ 100 000) — the number of rooms in the hotel and the number of cows travelling with Farmer John.

    The second line contains a string of length n describing the rooms. The i-th character of the string will be '0' if the i-th room is free, and '1' if the i-th room is occupied. It is guaranteed that at least k + 1 characters of this string are '0', so there exists at least one possible choice of k + 1 rooms for Farmer John and his cows to stay in.

    Output

    Print the minimum possible distance between Farmer John's room and his farthest cow.

    Examples
    input
    7 2
    0100100
    output
    2
    input
    5 1
    01010
    output
    2
    input
    3 2
    000
    output
    1
    Note

    In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms.

    In the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2.

    In the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1.

    题意:给n个房间,0代表空,1代表满,有k头牛和一个人,人和牛的距离为人到最远的那头牛的距离,要求这个距离尽量小,问最小的距离是多少;

    思路:把空的房间的位置都放到另外一个数组里,在遍历相邻的k+1个房间,二分找出人住在哪里才能使距离最小;ps:写好一个稳定的二分真的是需要功力啊啊啊;

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e5+7;
    const int inf=1e9+2;
    char s[N];
    int a[N],k;
    int bis(int L,int R)
    {
        int mid,md,ld,rd,le=L,ri=R;
        while(le<=ri)
        {
            mid=(le+ri)>>1;
            md=max(a[R]-a[mid],a[mid]-a[L]);
            if(mid-1>=L)ld=max(a[R]-a[mid-1],a[mid-1]-a[L]);
            else ld=md;
            if(mid+1<=R)rd=max(a[R]-a[mid+1],a[mid+1]-a[L]);
            else rd=md;
            if(md<=ld&&md<=rd)return md;
            else if(ld>=md&&md>rd)le=mid+1;
            else if(ld>=md&&md==rd)le=mid;
            else if(ld<md&&md<=rd)ri=mid-1;
            else if(ld==md&&md<=rd)ri=mid;
        }
        return md;
    }
    int main()
    {
        int n,cnt=1;
        cin>>n>>k;
        scanf("%s",s+1);
        for(int i=1;i<=n;i++)
        {
           if(s[i]=='0')
           {
               a[cnt++]=i;
           }
        }
        int ans=inf;
        for(int i=1;i+k<cnt;i++)
        {
            ans=min(ans,bis(i,i+k));
        }
        cout<<ans<<"
    ";
        return 0;
    }
  • 相关阅读:
    mouse without borders无界鼠标使用教程
    动态令牌-(OTP,HOTP,TOTP)-基本原理
    sha256C代码例子
    常用的前端设计工具分享
    PHP获取搜索引擎关键字来源(百度、谷歌、雅虎、搜狗、搜搜、必应、有道)
    为 Web 设计师准备的 25+ 款扁平 UI 工具包
    万能字段使用技巧整理
    css中overflow:hidden的属性 可能会导致js下拉菜单无法显示
    QQ空间g_tk加密算法PHP版
    QQ聊天机器人for PHP版 (登录,收、发消息)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5294971.html
Copyright © 2011-2022 走看看