Codeforces 645C:Enduring Exodus (二分+前缀和)
2017-03-02 00:16
381 查看
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
output
input
output
input
output
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 is2.
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.
给定01串,将k头牛和农夫放进, 0表示可以放进,1表示不可放进,求农夫距离其牛的最大距离的最小值。
思路:二分枚举最大值, 检验的时候, 因为是最大值,枚举人的位置用右面能到达的最远的地方-左面能到达最远的地方是不是有k+1个零给他们住就好了、、如果这样都不能住下就是不可能住下了。。为了减少复杂度用前缀和记录0的个数,这个蛮好想到的。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 5;
char str[maxn];
int n, k, cnt[maxn];
int check(int x)
{
for(int i = 1; i <= n; i++)
{
if(str[i] == '1') continue;
int l = max(1, i-x);
int r = min(n, i+x);
if(cnt[r]-cnt[l-1] >= k+1) return 1;
}
return 0;
}
int main()
{
cin >> n >> k;
scanf(" %s", str+1);
for(int i = 1; i <= n; i++)
{
cnt[i] = cnt[i-1];
if(str[i] == '0') cnt[i]++;
}
int l = 1, r = n, mid, ans;
while(l <= r)
{
mid = (l+r)/2;
if(check(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
printf("%d\n", ans);
return 0;
}
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 2000
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 is2.
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.
题意:
给定01串,将k头牛和农夫放进, 0表示可以放进,1表示不可放进,求农夫距离其牛的最大距离的最小值。思路:二分枚举最大值, 检验的时候, 因为是最大值,枚举人的位置用右面能到达的最远的地方-左面能到达最远的地方是不是有k+1个零给他们住就好了、、如果这样都不能住下就是不可能住下了。。为了减少复杂度用前缀和记录0的个数,这个蛮好想到的。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 5;
char str[maxn];
int n, k, cnt[maxn];
int check(int x)
{
for(int i = 1; i <= n; i++)
{
if(str[i] == '1') continue;
int l = max(1, i-x);
int r = min(n, i+x);
if(cnt[r]-cnt[l-1] >= k+1) return 1;
}
return 0;
}
int main()
{
cin >> n >> k;
scanf(" %s", str+1);
for(int i = 1; i <= n; i++)
{
cnt[i] = cnt[i-1];
if(str[i] == '0') cnt[i]++;
}
int l = 1, r = n, mid, ans;
while(l <= r)
{
mid = (l+r)/2;
if(check(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
printf("%d\n", ans);
return 0;
}
相关文章推荐
- Codeforces 645C Enduring Exodus 【二分】
- Codeforces 645C Enduring Exodus【二分】
- Codeforces--645C--Enduring Exodus(二分)
- Codeforces 645C Enduring Exodus【二分】
- CodeForces - 645C Enduring Exodus (二分)
- 【打CF,学算法——三星级】CodeForces 645C Enduring Exodus (二分+贪心)
- codeforces 655C C. Enduring Exodus(二分)
- CodeForces 645C Enduring Exodus
- CodeForces 645C Enduring Exodus
- Codeforces 645C:Enduring Exodus
- CodeForces - 645C Enduring Exodus
- Codeforces 479E Riding in a Lift【Dp+前缀和优化+二分】好题~
- CodeForces 460C——二分+前缀和—— Present
- codeForces 660C.Hard Process(二分+前缀和)
- Codeforces 676C - Vasya and String 详解(二分前缀和+尺取两种写法)
- Code Forces 645C Enduring Exodus
- CodeForces 645 C.Enduring Exodus(水~)
- codeforces 776C Molly's Chemicals(前缀和 二分搜索)
- Code Forces 645C Enduring Exodus
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))C. Enduring Exodus(二分)