题目链接:

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

题意:给n个房间,0代表空,1代表满,有k头牛和一个人,人和牛的距离为人到最远的那头牛的距离,要求这个距离尽量小,问最小的距离是多少;

思路:把空的房间的位置都放到另外一个数组里,在遍历相邻的k+1个房间,二分找出人住在哪里才能使距离最小;ps:写好一个稳定的二分真的是需要功力啊啊啊;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
const int inf=1e9+;
char s[N];
int a[N],k;
int bis(int L,int R)
{
int mid,md,ld,rd,le=L,ri=R;
while(le<=ri)
{
mid=(le+ri)>>;
md=max(a[R]-a[mid],a[mid]-a[L]);
if(mid->=L)ld=max(a[R]-a[mid-],a[mid-]-a[L]);
else ld=md;
if(mid+<=R)rd=max(a[R]-a[mid+],a[mid+]-a[L]);
else rd=md;
if(md<=ld&&md<=rd)return md;
else if(ld>=md&&md>rd)le=mid+;
else if(ld>=md&&md==rd)le=mid;
else if(ld<md&&md<=rd)ri=mid-;
else if(ld==md&&md<=rd)ri=mid;
}
return md;
}
int main()
{
int n,cnt=;
cin>>n>>k;
scanf("%s",s+);
for(int i=;i<=n;i++)
{
if(s[i]=='')
{
a[cnt++]=i;
}
}
int ans=inf;
for(int i=;i+k<cnt;i++)
{
ans=min(ans,bis(i,i+k));
}
cout<<ans<<"\n";
return ;
}

codeforces 655C C. Enduring Exodus(二分)的更多相关文章

  1. CROC 2016 - Elimination Round (Rated Unofficial Edition) C. Enduring Exodus 二分

    C. Enduring Exodus 题目连接: http://www.codeforces.com/contest/655/problem/C Description In an attempt t ...

  2. CodeForces - 645 C.Enduring Exodus

    快乐二分 用前缀和随便搞一下 #include <cstdio> using namespace std; ; int p[N]; ; inline int msum(int a, int ...

  3. Code Forces 645C Enduring Exodus

    C. Enduring Exodus time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...

  4. Enduring Exodus CodeForces - 655C (二分)

    链接 大意: n个房间, 1为占用, 0为未占用, John要将k头奶牛和自己分进k+1个空房间, 求John距最远的奶牛距离的最小值 这种简单题卡了20min.... 显然对于固定的k+1个房间, ...

  5. Codeforces 645C Enduring Exodus【二分】

    题目链接: http://codeforces.com/contest/645/problem/C 题意: 给定01串,将k头牛和农夫放进, 0表示可以放进,1表示不可放进,求农夫距离其牛的最大距离的 ...

  6. codeforces 645C . Enduring Exodus 三分

    题目链接 我们将所有为0的位置的下标存起来. 然后我们枚举左端点i, 那么i+k就是右端点. 然后我们三分John的位置, 找到下标为i时的最小值. 复杂度 $ O(nlogn) $ #include ...

  7. CodeForces 645C Enduring Exodus

    枚举,三分. 首先,这$n+1$个人一定是连续的放在一起的.可以枚举每一个起点$L$,然后就是在$[L,R]$中找到一个位置$p$,使得$p4最优,因为越往两边靠,距离就越大,在中间某位置取到最优解, ...

  8. CodeForces 377B---Preparing for the Contest(二分+贪心)

    C - Preparing for the Contest Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  9. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

随机推荐

  1. NYOJ-欧几里得

    欧几里得 时间限制:1000 ms  |  内存限制:65535 KB 难度:0 描写叙述 已知gcd(a,b)表示a,b的最大公约数. 如今给你一个整数n,你的任务是在区间[1,n)里面找到一个最大 ...

  2. ubuntu下搭建的lamp环境新建站点

    这几天刚装了一个ubuntu 16.04桌面版,总之来来回回几遍才基本把环境搭建好,本来用apt-get搭建,结果不知道什么原因16.04版不支持装php5 ,提示源放弃了php5版本,不得不使用ph ...

  3. table中tr的display属性在火狐中显示不正常,IE中显示正常

    最近在作项目的时候碰到一个问题,就是需要AJAX来交互显示<tr> </tr> 标签内的东西,按照常理,对于某一单元行需要显示时,使用:display:block属性,不需要显 ...

  4. Unity3d 快捷键

    Windows系统Unity3D中的快捷键 组合键 键 功能 File 文件 Ctrl   N New Scene 新建场景 Ctrl   O Open Scene 打开场景 Ctrl   S Sav ...

  5. 【原创】Hibernate自动生成(2)

    本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正. 开发环境: Eclipse Version: Photon ...

  6. COGS 1507. [IOI2000]邮局

    1507. [IOI2000]邮局 ★☆   输入文件:postoffice.in   输出文件:postoffice.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] ...

  7. Eclipse下使用maven搭建多模块项目

    暂时将项目分为如下几层: domain(域模型层).dao(数据库访问层).service(业务逻辑层).web(表现层),有需要再另行添加(如common等): 目录结构: 一.app 该层为父层, ...

  8. Django多对多的创建

    1.多对多创建的应用场景: 在某表中创建一行数据是,有一个可以多选的下拉框 例如:创建用户信息,需要为用户指定多个爱好 2.创建方式: 方式一:自定义关系表,手动创建一张表用于关联其他多张表的关系 c ...

  9. Data Structure Array: Maximum of all subarrays of size k

    http://www.geeksforgeeks.org/maximum-of-all-subarrays-of-size-k/ #include <iostream> #include ...

  10. python 3 并发编程之多进程 multiprocessing模块

    一 .multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程. ...