• CROC 2016


    C. Enduring Exodus

    题目连接:

    http://www.codeforces.com/contest/655/problem/C

    Description

    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.

    Sample Input

    7 2
    0100100

    Sample Output

    2

    Hint

    题意

    有7个房间,现在A先生带着k个妹子来住酒店,每个人一个房间

    房间为1表示不可预定,为0表示可以预定。

    然后A先生希望预定房间之后,他离最远的妹子最近,问你这个距离是多少。

    题解:

    比较显然就是二分+O(n)去check就好了

    check的时候,我暴力枚举A先生住在哪儿就好了,然后用一个前缀和什么玩意儿去维护一下就好了。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+7;
    int n,k;
    char s[maxn];
    int sum[maxn];
    bool check(int mid)
    {
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='1')continue;
            int l = max(i-mid,1);
            int r = min(i+mid,n);
            if((sum[r]-sum[l-1]-1)>=k)return true;
        }
        return false;
    }
    int main()
    {
        cin>>n>>k;
        scanf("%s",s+1);
        for(int i=1;i<=n;i++)
        {
            sum[i]=sum[i-1];
            if(s[i]=='0')sum[i]++;
        }
        int l = 0,r = n+100,ans = 0;
        while(l<=r)
        {
            int mid = (l+r)/2;
            if(check(mid))r=mid-1,ans=mid;
            else l=mid+1;
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    HBase- 安装单机版HBase
    javascript中的设计模式之模板方法模式
    win 设置自动启动软件
    php高精度加减乘除
    frp实现内网穿透,实现夸服务器访问
    OCM 12c 直考预备知识点
    Oracle 19c New Features : Active Data Guard DML Redirect
    3级搭建类302-Oracle 19c RAC 双节点搭建
    VMWare WorkStation 15.5 配置RAC共享存储节点二无法识别共享磁盘UUID解决办法
    你还在争论 count(*) 与 count(column) 哪个更快?
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5300195.html
Copyright © 2020-2023  润新知