Codeforces 645C:Enduring Exodus
2016-03-22 22:10
441 查看
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 is 2.
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.
题意是john带着k个牛进旅馆,旅馆房间的情况是0代表是空房间,1代表是有人住了。问把john和牛都安排好,john和牛的最远距离。
枚举john所在位置,二分其最大距离。用前缀和判断两头有没有k+1个房间。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define INF 0x333f3f3f
const ll mod = 1000000007;
const int maxn = 100005;
const double PI = acos(-1.0);
int n, k;
int pre[maxn], pos[maxn];
char x[maxn];
bool check(int pos, int mid)
{
int ri = min(n, pos + mid);
int le = max(1, pos - mid);
return pre[ri] - pre[le - 1] >= k + 1;
}
void solve()
{
int i, j, nx;
cin >> n >> k >> x + 1;
for (i = 1; i <= n; i++)
{
if (x[i] == '0')
{
pre[i] = pre[i - 1] + 1;
}
else
{
pre[i] = pre[i - 1];
}
}
int le, ri, mid;
int ans = n;
for (i = 1; i <= n; i++)
{
if (x[i] == '1')continue;
le = 1, ri = n;
while (le < ri)
{
mid = (le + ri) >> 1;
if (check(i, mid))
{
ri = mid;
}
else
{
le = mid + 1;
}
}
ans = min(ans, ri);
}
cout << ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
#endif
solve();
//system("pause");
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 is 2.
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.
题意是john带着k个牛进旅馆,旅馆房间的情况是0代表是空房间,1代表是有人住了。问把john和牛都安排好,john和牛的最远距离。
枚举john所在位置,二分其最大距离。用前缀和判断两头有没有k+1个房间。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define INF 0x333f3f3f
const ll mod = 1000000007;
const int maxn = 100005;
const double PI = acos(-1.0);
int n, k;
int pre[maxn], pos[maxn];
char x[maxn];
bool check(int pos, int mid)
{
int ri = min(n, pos + mid);
int le = max(1, pos - mid);
return pre[ri] - pre[le - 1] >= k + 1;
}
void solve()
{
int i, j, nx;
cin >> n >> k >> x + 1;
for (i = 1; i <= n; i++)
{
if (x[i] == '0')
{
pre[i] = pre[i - 1] + 1;
}
else
{
pre[i] = pre[i - 1];
}
}
int le, ri, mid;
int ans = n;
for (i = 1; i <= n; i++)
{
if (x[i] == '1')continue;
le = 1, ri = n;
while (le < ri)
{
mid = (le + ri) >> 1;
if (check(i, mid))
{
ri = mid;
}
else
{
le = mid + 1;
}
}
ans = min(ans, ri);
}
cout << ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
#endif
solve();
//system("pause");
return 0;
}
相关文章推荐
- Java for循环的几种写法
- Activity_Class_Fragments
- cookie 和session 的区别详
- 【poj 3126】Prime Path 题意&题解&代码(C++)
- ExtJS之Ext.getDom
- [转载]Java静态函数、父类、子类执行顺序
- 一个appium 博客
- 结对编程——词频统计
- Epoll在LT和ET模式下的读写方式
- Android获取手机本地图片缩略图
- python之bashlib模块
- 文件重命名或者项目重命名
- [转]Ubuntu 12.04(32位)安装Oracle 11g(32位)全过程以及几乎所有问题的解决办法
- Apache CXF REST 文件上传,中文乱码(已解决)
- 小白的282旅程(第一例)
- 《JAVA语言程序设计》(三)|创建、编译和执行java程序
- POJ 3414 BFS
- 【Design Pattern】C++ 线程安全的单例模式
- 第一个java程序HelloWorld.java
- windows客户端开发--使用tinyxml库解析xml文件