C. Enduring Exodus

题目连接:

http://www.codeforces.com/contest/655/problem/C

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

7 2

0100100

Sample Output

2

Hint

题意

有7个房间,现在A先生带着k个妹子来住酒店,每个人一个房间

房间为1表示不可预定,为0表示可以预定。

然后A先生希望预定房间之后,他离最远的妹子最近,问你这个距离是多少。

题解:

比较显然就是二分+O(n)去check就好了

check的时候,我暴力枚举A先生住在哪儿就好了,然后用一个前缀和什么玩意儿去维护一下就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n,k;
char s[maxn];
int sum[maxn];
bool check(int mid)
{
for(int i=1;i<=n;i++)
{
if(s[i]=='1')continue;
int l = max(i-mid,1);
int r = min(i+mid,n);
if((sum[r]-sum[l-1]-1)>=k)return true;
}
return false;
}
int main()
{
cin>>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+100,ans = 0;
while(l<=r)
{
int mid = (l+r)/2;
if(check(mid))r=mid-1,ans=mid;
else l=mid+1;
}
cout<<ans<<endl;
}

CROC 2016 - Elimination Round (Rated Unofficial Edition) C. Enduring Exodus 二分的更多相关文章

  1. CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 二分+拓扑排序

    D. Robot Rapping Results Report 题目连接: http://www.codeforces.com/contest/655/problem/D Description Wh ...

  2. CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 拓扑排序+二分

    题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值 ...

  3. CROC 2016 - Elimination Round (Rated Unofficial Edition) F - Cowslip Collections 数论 + 容斥

    F - Cowslip Collections http://codeforces.com/blog/entry/43868 这个题解讲的很好... #include<bits/stdc++.h ...

  4. CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp

    E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...

  5. CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp

    E. Intellectual Inquiry 题目连接: http://www.codeforces.com/contest/655/problem/E Description After gett ...

  6. CROC 2016 - Elimination Round (Rated Unofficial Edition) B. Mischievous Mess Makers 贪心

    B. Mischievous Mess Makers 题目连接: http://www.codeforces.com/contest/655/problem/B Description It is a ...

  7. CROC 2016 - Elimination Round (Rated Unofficial Edition) A. Amity Assessment 水题

    A. Amity Assessment 题目连接: http://www.codeforces.com/contest/655/problem/A Description Bessie the cow ...

  8. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

  9. 8VC Venture Cup 2016 - Elimination Round D. Jerry's Protest 暴力

    D. Jerry's Protest 题目连接: http://www.codeforces.com/contest/626/problem/D Description Andrew and Jerr ...

随机推荐

  1. java类中访问属性

    package first; public class for_protect { private int age=10; int number = 100; public void show(){ ...

  2. linux的防火墙管理

    换oricle-linux7系统后,发现iptables的管理方法有不小的改动,记录一下遇到的问题. iptables linux系统已经默认安装了iptables和firewalld两款防火墙管理工 ...

  3. Mysql存储之ORM框架SQLAlchemy(一)

    上一篇我们说了mysql存储的原生语句方式,因为原生语句每次写都比较的复杂,所以这里我们说一种引用实体类的方式来操作数据库. 什么是ORM ORM技术:Object-Relational Mappin ...

  4. 设计模式之笔记--代理模式(Proxy)

    代理模式(Proxy) 定义 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. 类图 描述 Subject,定义了ConcreteSubject和Proxy的共用接口,这样就可以 ...

  5. python模块之itertools

    在循环对象和函数对象中,我们了解了循环器(iterator)的功能.循环器是对象的容器,包含有多个对象.通过调用循环器的next()方法 (__next__()方法,在Python 3.x中),循环器 ...

  6. sort排序命令常见用法

    sort -n 按数字排序 [root@test88 ~]# cat test.txt 19036 6111 24039 3660 20610 10937 32408 20744 8248 28255 ...

  7. 使用Jackson来实现Java对象与JSON的相互转换的教程

    一.入门Jackson中有个ObjectMapper类很是实用,用于Java对象与JSON的互换.1.JAVA对象转JSON[JSON序列化] 1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  8. Percona XtraDB Cluster(PXC)-高可用架构设计说明

    Mycat+PXC高可用集群 一.架构图 架构说明: 1.mysql 集群高可用部分: l 针对业务场景选用Percona XtraDB Cluter(PXC)复制集群.两个片集群 PXC-dataN ...

  9. python datetime 时区(timezone) dateutil

    记录下python中的时区问题, 代码如下:  包括datetime.datetime对象使用不同的时区,以及在不同时区间转换. from datetime import datetime from ...

  10. Android Webview中解决H5的音视频不能自动播放的问题

    在开发webview的时候,当加载有声音的网页的时候,声音不会自动播放, 解决方法:在webview中调用js方法.这个方法需要在webview的setWebViewClient方法之后在onPage ...