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

农场主约翰带着他的奶牛们旅行,并计划入住一家旅馆。他们需要找到连续的k+1个空闲房间,使得约翰与最远的奶牛之间的距离尽可能小。通过二分搜索来解决这一问题。
摘要由CSDN通过智能技术生成
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 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.

#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|1
typedef 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[N];
int sum[N];
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)

*/


Key Features The problems of CSS at scale - specificity, the cascade and styles intrinsically tied to element structure. The shortfalls of conventional approaches to scaling CSS. The ECSS methodology and the problems it solves. How to develop consistent and enforceable selector naming conventions with ECSS. How to organise project structure to more easily isolate and decouple visual components. Considerations of CSS tooling and processing: Sass/PostCSS and linting. Addressing the notion of CSS selector speed with hard data and browser representative insight. Book Description Learn with me, Ben Frain, about how to really THINK about CSS and how to use CSS for any size project! I'll show you how to write CSS that endures continual iteration, multiple authors, and yet always produces predictable results. Enduring CSS, often referred to as ECSS, offers you a robust and proven approach to authoring and maintaining style sheets at scale. Enduring CSS is not a book about writing CSS, as in the stuff inside the curly braces. This is a book showing you how to think about CSS, and be a smarter developer with that thinking! It's about the organisation and architecture of CSS: the parts outside the braces. I will help you think about the aspects of CSS development that become the most difficult part of writing CSS in larger projects. You ll learn about the problems of authoring CSS at scale - including specificity, the cascade and styles intrinsically tied to document structure. I'll introduce you to the ECSS methodology, and show you how to develop consistent and enforceable selector naming conventions. We'll cover how to apply ECSS to your web applications and visual models. And how you can organise your project structure wisely, and handle visual state changes with ARIA, providing greater accessibility considerations. In addition, we'll take a deep look into CSS tooling and process considerations. Finally we will address performance considerations by examining topics such as CSS selector speed with hard data and browser-representative insight. Enduring CSS is a book for considering how to write CSS in the most effective manner, and how you too can create an enduring CSS code base, regardless of project size. Take a journey with me if you want to explore these topics and deepen your thinking as a CSS author.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值