• Codeforces 645C 二分


    题目:
    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 n rooms 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.
    题意:
    有个人带着k个母牛去开房,只有0的房间可以住,要求这个人住的房间和离他最远的那头牛的距离最小,找出最小距离。
    分析:
    用前缀和求出前i个房间有多少个空房。然后就要找k+1个空房间,找出k+1个后,这个人当然是住在中间解最优啦,但是中间有可能是1,所以要判断一下。

    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+2;
    typedef long long ll;
    int n,m;
    int a[N];
    char s[N];
    int main()
    {
        //freopen("f.txt","r",stdin);
        cin>>n>>m;
        scanf("%s",s);
        a[0]=0;
        for(int i=1;i<=n;i++){
            if(s[i-1]=='0')a[i]=a[i-1]+1;
            else a[i]=a[i-1];
        }
        int ans=n;
        for(int i=1;i<=n;i++){
            if(s[i-1]=='0'){
                int *p=lower_bound(a+i,a+1+n,a[i]+m);
                if(*p==0)break;
                int k=p-(a+i);
                int t=k/2+i;
                int tt=t;
                while(s[tt-1]!='0')tt++;
                ans=min(ans,max(tt-i,i+k-tt));
                tt=t;
                while(s[tt-1]!='0')tt--;
                ans=min(ans,max(tt-i,i+k-tt));
            }
        }
        cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    SQL舊面版與標准版不能互導數據
    今天總算可以繼續寫日記了
    MDAC2.7没有安装,控件不能运行
    希望5月份网络能开通
    SQLITE入门逐步讲解SQLITE命令行(一)
    C#使用SQLite数据库的代码示例
    红外遥控 Arduino 实例
    SQLite数据库是中小站点CMS的最佳选择
    防止 SQLite 数据库被下载的方法
    关于Linux下的Grep命令简介
  • 原文地址:https://www.cnblogs.com/01world/p/5651237.html
Copyright © 2020-2023  润新知