题目链接:

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. Gradle 构建工具

    参考文章: 作者:ghui 链接:https://www.zhihu.com/question/30432152/answer/48239946 来源:知乎 著作权归作者所有.商业转载请联系作者获得授 ...

  2. 多媒体开发之rtp 打包发流---udp 丢包问题

    http://blog.csdn.net/acs713/article/details/19339707

  3. html5小趣味知识点系列(一)spellcheck

    发现一些h5的系ode知识点增加一些趣味性 实用性 不敢妄自评论  觉得有用就用一下  没用就路过一下 spellcheck属性 它的功能是针对用户输入的文本内容进行拼写和语法检查 用于input 和 ...

  4. ASP.NET动态网站制作(23)-- ADO.NET(2)

    前言:这节课老师请高级班的E老师过来代课,还是接着老师讲的内容继续深入,修改了上节课老师写的部分代码. 内容: 1.数据库本质就是一个软件,这个软件帮助我们把数据有序地存储起来,当我们需要数据的时候帮 ...

  5. 微信小程序事件

    微信小程序事件1.什么是事件2.事件类别3.事件冒泡4.事件绑定5.事件对象详解笔记:1.事件是一种用户的行为,是一种通讯方式.2.事件类别:    点击事件:tap    长按事件:longtap  ...

  6. Spring框架结构

    在processOn思维导图上看的一个程序员写的,挺不错的,分享出来,便于学习和回顾.

  7. Jmeter 04 JMeter 负载与监听

    1. 场景设计 2. 场景设置 3. JMeter性能参数配置 4. 测试监听

  8. hdu 4414 Finding crosses【简单模拟】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...

  9. vue v-on命令

    <!-- 阻止单击事件冒泡 --> <a v-on:click.stop="doThis"></a>   <!-- 提交事件不再重载页面 ...

  10. Python菜鸟之路:sqlalchemy/paramiko进阶

    前言:ORM中的两种创建方式 数据库优先:指的是先创建数据库,包括表和字段的建立,然后根据数据库生成ORM的代码,它是先创建数据库,再创建相关程序代码 代码优先:就是先写代码,然后根据代码去生成数据库 ...