CROC 2016 - Elimination Round C 二分




链接:戳这里


C. Enduring Exodus
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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只奶牛投宿,酒店有n个房间,但是有一些房间是住了人的,给出n,k (1<=k<n<=1e5)  给出长度为n的01序列,0代表房间没有人居住,1代表有人居住,农夫想要奶牛尽可能的安全,所以他选择住在一个房间里面满足到最远的奶牛房间的距离最小  距离=|j-i| 也就是下标


思路:嗯 首先二分这个距离,拿这个距离去判断是否有一个房间让农夫住进去之后可以再这个距离内处理奶牛问题,取出最小的这个距离  O(nlogn)


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1e9
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,k;
char s[1000100];
int a[1000100],sum[1000100];
bool solve(int x,int pos){
    int l=max(1,pos-x);
    int r=min(n,pos+x);
    if(sum[r]-sum[l-1]>=k)
        return true;
    return false;
}
int main(){
    scanf("%d%d",&n,&k);
    k++;
    scanf("%s",s+1);
    for(int i=1;i<=n;i++) a[i]=s[i]-'0';
    if(a[1]==0) sum[1]=1;
    else sum[1]=0;
    for(int i=2;i<=n;i++){
        if(a[i]==0) sum[i]++;
        sum[i]+=sum[i-1];
    }
    ///for(int i=1;i<=n;i++) cout<<sum[i]<<" ";
    ///cout<<endl;
    int ans=MAX;
    for(int i=1;i<=n;i++){
        if(a[i]==1) continue;
        int t=n,l=1,r=n,mid;
        while(l<r){
            mid=(l+r)/2;
            if(solve(mid,i)) r=mid,t=mid;
            else l=mid+1;
        }
        ///cout<<t<<endl;
        ans=min(ans,t);
    }
    cout<<ans<<endl;
    return 0;
}
/*
112 12
0110101000000010101110010111100101011010011110100111111100011101011111000111101101110100111011110001100110110010
10
*/


你可能感兴趣的:(CROC 2016 - Elimination Round C 二分)