超简单好理解 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 iand 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+1个0(被1隔开也算连续,被0隔开则不连续),然后把人放在尽可能中间的房子,记录所有0的位置,a[i]表示第i个0的位置,每次在a[i]和a[i+k]中二分查找最接近a[i]+(a[i+k]-a[i])/2的点pos作为人的位置然后取所有max(a[i+k]-pos,pos-a[i])中的最小值即为答案 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace  std;
#define maxn 100005
char s[maxn];
int a[maxn];
int main()
{
    int  i , j, n, k;
    while( scanf("%d%d", &n, &k ) != EOF )
    {
        memset( s, 0, sizeof( s ));
        memset( a, 0, sizeof( a ));
        int cnt = 0;
        for( i = 0 ; i<n; i++)
        {
            cin>>s[i];
            if( s[i] == '0' )
            {
                a[cnt++] = i;
                //cout<<i<<endl;
            }
        }
        //sort( a, a+cnt);
        int ans = maxn;
        for( i = 0 ; i<cnt-k; i++)
        {
            int t = (a[i+k]+a[i])/2;
            //cout<<t<<endl;
            int pos = lower_bound( a, a+cnt , t)-a;
            //printf("%d\n", pos);
            ans = min( ans, max( a[pos] - a[i] , a[i+k]-a[pos]));
            //cout<<ans<<endl;
            pos--;///这是为了防止取前一个较小值结果更小的情况
        ans = min( ans, max( a[pos] - a[i] ,a[i+k] - a[pos]));比如 0的位置 1 3 4 7 8 9 则选取4比7更合适 
         }
         printf("%d\n", ans);
    }
}
强大的lower_bound!!

自我感觉讲的不明白  语言功底太差 wwwwwwwww

这是那位大佬的博客地址

https://blog.csdn.net/v5zsq/article/details/70894114

猜你喜欢

转载自blog.csdn.net/jianxingzhang/article/details/80055150