我是靠谱客的博主 务实钢笔,最近开发中收集的这篇文章主要介绍CodeForces 645C Enduring Exodus,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题意:有n个房间,现在有k只牛和你来住,其中0代表空闲,1代表已经有人了,求大家相距的距离的最大值要最小。

思路:最值最小或者最大问题,显然二分答案,然后直接暴力check即可


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100001
#define LL long long
int cas=1,T;
int n,k;
char s[maxn];
int sum[maxn];
bool check(int m)
{
	for (int i = 1;i<=n;i++)
	{
		if (s[i]=='1')
			continue;
		int l = max(i-m,1);
		int r = min(i+m,n);
		if (sum[r]-sum[l-1]-1>=k)return true;
	}
	return false;
}
int main()
{
	scanf("%d%d",&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,ans=0;
	while (l<=r)
	{
		int m = (l+r)/2;
		if (check(m))
			r=m-1,ans=m;
		else
			l=m+1;
	}
	printf("%dn",ans);
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

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

Sample Input

Input
7 2
0100100
Output
2
Input
5 1
01010
Output
2
Input
3 2
000
Output
1


最后

以上就是务实钢笔为你收集整理的CodeForces 645C Enduring Exodus的全部内容,希望文章能够帮你解决CodeForces 645C Enduring Exodus所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部