您的位置:首页 > 其它

【CROC 2016 - Elimination RoundC】【二分】Enduring Exodus 最小的人看守与羊距离

2016-03-19 12:03 375 查看
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 2000


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.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1#define rs o<<1|1typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, k;
char s
;
int sum
;
int check(int p,int dis)
{
int l = max(1, p - dis);
int r = min(n, p + dis);
return (sum[r] - sum[l - 1] - 1) >= k;
}
int main()
{
while (~scanf("%d%d", &n,&k))
{
scanf("%s", s + 1);
for (int i = 1; i <= n; ++i)sum[i] = sum[i - 1] + (s[i] == '0');
int ans = n;
for (int i = 1; i <= n; ++i)if (s[i] == '0')
{
int l = 1;
int r = n;
while (l < r)
{
int mid = (l + r) / 2;
if (check(i,mid))r = mid;
else l = mid + 1;
}
gmin(ans, l);
}
printf("%d\n", ans);
}
return 0;
}
/*
【trick&&吐槽】
最大的最小,往往是二分的标志

【题意】
有一个旅馆,房间有n(1e5)个,1代表有人住了,0代表空余。
我们需要住1个看守和k只羊。保证住得下。
问最优安排下,看守与羊最大距离的最小值是多少

【类型】
二分

【分析】
直接枚举人的位置,然后二分最大距离,检测是否住得下
这题也有set做法,稍微麻烦一些。

【时间复杂度&&优化】
O(nlogn)

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息