Codeforces--645C--Enduring Exodus(二分)
2016-04-22 15:50
381 查看
Enduring Exodus
Submit Status
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
Input
Output
Input
Output
Input
Output
Time Limit: 2000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
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
Input
7 2 0100100
Output
2
Input
5 1 01010
Output
2
Input
3 2000
Output
1
题意:有一个人去住旅店,随身带了k头牛,旅店有n个房间,0表示没人,1表示有人这个人想尽可能的让自己跟牛住得近,求最远的牛跟他之间的距离
思路:先记录前缀和,然后二分枚举最大长度,判断这个长度是否可以,因为求的是最短的距离所以判断稍微改一下
#include<cstdio> #include<cstring> #include<queue> #include<cmath> #include<algorithm> using namespace std; int n,k,dp[100000+100]; char s[100000+100]; bool judge(int x) { for(int i=1;i<=n;i++) { if(s[i]=='0') { int l=i-x; int r=i+x; if(l<=1) l=1; if(r>=n) r=n; if(dp[r]-dp[l-1]-1>=k) { return true; } } } return false; } int erfen(int l,int r) { int ans=0; while(l<=r) { int mid=(l+r)/2; if(judge(mid)) { ans=mid; r=mid-1; } else l=mid+1; } return ans; } int main() { while(scanf("%d%d",&n,&k)!=EOF) { memset(s,0,sizeof(s)); scanf("%s",s+1); memset(dp,0,sizeof(dp)); dp[0]=0; for(int i=1;i<=n;i++) { if(s[i]=='0') dp[i]=dp[i-1]+1; else dp[i]=dp[i-1]; } printf("%d\n",erfen(0,n)); } return 0; }
相关文章推荐
- MySQL审计之init-connect + binlog
- Unity5x 场景烘焙,动态物体烘焙不上
- spring 加载xml
- bootstrap介绍
- hibernate--多对多双向关联(少用)
- 微软推荐您完成Windows 10 的免费升级
- java 使用 comet4j 主动向客户端推送信息 简单例子
- Android studio入门详细教程,首次启动配置,不翻墙搞定
- Linux环境下部署完JDK后运行一个简单的Java程序
- OpenCV_连通区域分析(Connected Component Analysis/Labeling)
- jetty服务器,虚拟目录设置总结!
- 编写代码期间遇到的问题
- 搭建ExtJS环境
- CANopen系列文章【1】--SYNC【2】
- Round函数
- 作业调度模拟程序
- 正则中需要转义的特殊字符小结
- 三、svn--权限设置
- 利用STL中的队列queue对二叉树进行层遍历
- [LintCode] 带最小值操作的栈 Min Stack