(CROC 2016 - Elimination Round (Rated Unofficial Edition))C. Enduring Exodus(二分)
来源:互联网 发布:淘宝改后台软件安全吗 编辑:程序博客网 时间:2024/04/27 23:23
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.
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.
Print the minimum possible distance between Farmer John's room and his farthest cow.
7 20100100
2
5 101010
2
3 2000
1
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.
题意:
有n个房间,现在有农夫带着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(二分)
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))D. Robot Rapping Results Report(二分+拓扑排序)
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))B. Mischievous Mess Makers(贪心)
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))A. Amity Assessment(贪心)
- CROC 2016 - Elimination Round (Rated Unofficial Edition)题解
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp
- 【CROC 2016 - Elimination RoundC】【二分】Enduring Exodus 最小的人看守与羊距离
- Codeforces--645C--Enduring Exodus(二分)
- CodeForces - 645C Enduring Exodus (二分)
- CROC 2016 - Elimination Round C 二分
- Codeforces 645C:Enduring Exodus (二分+前缀和)
- CodeForces 645 C.Enduring Exodus(水~)
- CROC 2016 - Elimination Round(A)bfs
- 【打CF,学算法——三星级】CodeForces 645C Enduring Exodus (二分+贪心)
- Codeforces 645C Enduring Exodus 【二分】
- Codeforces 645C Enduring Exodus【二分】
- CROC 2016 - Elimination Round D 二分+拓扑序+bfs
- Codeforces CROC 2016 - Elimination Round
- 会声会影打开出现已停止工作
- 1015. Reversible Primes (20)
- 探究iOS上拉、下拉原理
- 3D电视已死 4K和曲面屏成厂商下一个目标
- 关于MPMoviePlayerController类播放视频时,外放没有声音的问题
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))C. Enduring Exodus(二分)
- Android-在动作栏中添加“设置图标”
- 什么是C++虚函数、虚函数的作用和使用方法
- AutoCompleteTextView的使用和源码分析以及实现响应式输入提示功能
- Fixing your iOS build scripts PackageApplication ERROR
- 选择支持向量机(SVM)核函数
- Iframe父页面与子页面之间的相互调用
- 10.1Android性能优化之布局优化
- 第七届蓝桥杯省赛C/C++B组省赛题解