codeforces 645C

来源:互联网 发布:淘宝假冒品牌处罚 编辑:程序博客网 时间:2024/06/08 08:10
C. 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 hisk cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists ofn 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 andk (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. Thei-th character of the string will be '0' if thei-th room is free, and '1' if thei-th room is occupied. It is guaranteed that at leastk + 1 characters of this string are '0', so there exists at least one possible choice ofk + 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 20100100
Output
2
Input
5 101010
Output
2
Input
3 2000
Output
1
Note

In the first sample, Farmer John can book room 3 for himself, and rooms1 and 4 for his cows. The distance to the farthest cow is2. Note that it is impossible to make this distance1, as there is no block of three consecutive unoccupied rooms.

In the second sample, Farmer John can book room 1 for himself and room3 for his single cow. The distance between him and his cow is2.

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 is1.



早期a一题,虽然是水题。。。

题目意思是,有一长度为n的字符串,你需要找到m + 1个为0的位置,使得端点到中点的距离尽量小(中点必须为0),输出最小值。

分析,前缀和+二分肯定是没错的,不过这次不是枚举区间左端点二分区间右端点,而是枚举区间中点!!!!二分区间长度。写法很固定,以下是代码以及修改过的头文件

/*************************************************************************    > File Name: 645C.cpp    > Author: Triose    > Mail: Triose@163.com     > Created Time: 2016年06月08日 星期三 21时21分39秒 ************************************************************************/#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<algorithm>#include<vector>#include<queue>#include<stack>#include<iterator>#include<math.h>#include<stdlib.h>#include<time.h>#include<map>#include<set>using namespace std;//#define ONLINE_JUDGE#define eps 1e-8#define INF 0x7fffffff#define INFL 0x3f3f3f3f3f3f3f3fLL#define inf 0x3f3f3f3f#define rep(i,a,b) for(int i = (a); i < (b); ++i)#define repe(i,a,b) for(int i = (a); i <= (b); ++i)#define mem(a,b) (memset((a),b,sizeof(a)))#define sf(a) scanf("%d",&a)#define sfI(a) scanf("%I64d",&a)#define sfd(a,b) scanf("%d%d",&a,&b)#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sfs(a) scanf("%s",a)#define pf(a) printf("%d\n",a)#define pfd(a,b) printf("%d %d\n",a,b)#define pfs(a) printf("%s\n",a)#define pfI(a) printf("%I64d\n",a)#define enter putchar(10)#define LL long long#define DB doubleconst double PI = acos(-1.0);const double E = exp(1.0);template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }template<class T> inline T Min(T a, T b) { return a<b ? a : b; }template<class T> inline T Max(T a, T b) { return a>b ? a : b; }int n, m;#define N 100010int a[N];char str[N];int minn;bool check(int pos, int len) {    int left = Max(1, pos - len);    int right = Min(n, pos + len);    return a[right] - a[left - 1] >= m;}void brain_search(int th) {    int low = 1, high = n;    while(low <= high) {int mid = (low + high) >> 1;if(check(th, mid)) {    minn = Min(minn, mid);    high = mid - 1;}else {    low = mid + 1;}    }}int main() {#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);//  freopen("Out.txt", "w", stdout);#endif    while(~sfd(n,m)) {m++;sfs(str + 1);a[0] = 0;repe(i,1,n) a[i] = (1 - (str[i] - '0')) + a[i - 1];minn = INF;repe(i,1,n) {    if(str[i] == '1') continue;    brain_search(i);}pf(minn);    }    return 0;}



0 0
原创粉丝点击