Code Forces 645C Enduring Exodus
2016-04-20 22:03
381 查看
C. Enduring Exodus
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.
Examples
input
7 2
0100100
output
2
input
5 1
01010
output
2
input
3 2
000
output
1
先枚举人的位置,然后二分枚举人两侧的距离长度,
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.
Examples
input
7 2
0100100
output
2
input
5 1
01010
output
2
input
3 2
000
output
1
先枚举人的位置,然后二分枚举人两侧的距离长度,
#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include <stdio.h> #include <math.h> using namespace std; #define MAX 100000 char b[MAX+5]; int s[MAX+5]; int n,k; int main() { scanf("%d%d",&n,&k); scanf("%s",b+1); s[0]=0; for(int i=1;i<=n;i++) s[i]=s[i-1]+(b[i]=='0'?1:0); int ans=MAX*10; for(int i=1;i<=n;i++) { if(b[i]=='1') continue; int l=1,r=n; while(l<r) { int mid=(l+r)/2; int ml=max(1,i-mid); int mr=min(n,i+mid); if(s[mr]-s[ml-1]-1>=k) r=mid; else l=mid+1; } ans=min(ans,l); } printf("%d\n",ans); return 0; }
相关文章推荐
- 基于Qt的开源音乐播放器(CZPlayer)
- Eclipse和MyEclipse使用技巧--解决MyEclipse中的js报错的小方法
- 第三方登录之微信登录,基于ThinkSDK
- poj1047 数论
- 视频网络协议
- 第四周 时间
- Gson之基本用法
- window.location.hash详解
- Redis缓存实现单点登录SSO
- 由浅入深解析HashMap
- 20141223--全局使用局部变量+静态变量-02
- 网络通信解析
- 前端开发必备 - Emmet
- TransR/CTransR论文:链接预测
- javascript中的Left,Top,W 4000 idth,Height等相关属性汇总
- 快速排序(Quicksort)的Javascript实现
- windows下批量读取文件夹及子文件夹下的文件名字,方便制作训练样本
- 今天是我的第一次在github上发表博文 3ff8
- crossdomain.xml 文件安全配置 百度云观测、360网站监测 dz论坛crossdomain.xml
- sqlite3函数sqlite3_exec