CodeForces - 645C Enduring Exodus (二分)
2016-04-22 22:01
288 查看
CodeForces - 645C Enduring Exodus
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 Source CROC 2016 - Elimination Round //题意:输入n,m,接着输入一个长度为n的01串 表示一个人带着m头牛入住客栈,现在客栈里有n个房间,1表示已经有人住了,0表示没人住,现在问这个人和他的牛都入住客栈后,求这个人距离最远的那头牛的最短距离。 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> #include<set> #include<queue> #include<stack> using namespace std; #define mem(x,y) memset(x,y,sizeof(x)) const int MAXN = 100100; char s[MAXN]; int dp[MAXN]; int n, k; bool js(int x) { for(int i = 1; i <= n; i++) { if(s[i] == '0') { int l = i - x, 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 mid, ans = 0; while(l <= r) { mid = (l + r) >> 1; if(js(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } return ans; } int main() { while(~scanf("%d%d", &n, &k)) { scanf("%s", s + 1); dp[0] = 0; for(int i = 1; s[i]; 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; } |
相关文章推荐
- 事件的添加,触发,删除
- Delphi命令行输出组件DOSCommand及Widnows 命令解释使用说明
- spark源码阅读笔记RDD(二)RDD子类基本方法和信息
- 个人冲刺01,第八周进度条
- 对Mathsapp的测试以及找bug
- MapReduce输入格式
- 【BFS】初探
- Wiki
- E矿大获取密码测试
- 在windows下快速搭建web.py开发框架方法
- poj2524 Ubiquitous Religions
- Android 使用HttpGet进行登录
- Permutations
- JAVA动态规划(四)--根据给定0和1的个数,求字典序排在第K位的数【微软笔试题】
- 几年工作经验的我,到底应该具备哪些能力
- 团队项目
- 类图
- 团队项目-个人博客-典型用户分析
- JSP基础
- LeetCode208 Implement Trie (Prefix Tree) Java