Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Line 1: Three space-separated integers: L, N, and M
Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample
| Inputcopy | Outputcopy |
|---|---|
25 5 2 2 14 11 21 17 |
4 |
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
奶牛跳房子:从N块石头中移除M块,使得间距最小值最大。
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
const int N = 5e5 + 10;
int d, n, del;
int a[N];
int find(int x)
{
int j = 0, ans = 0; //j为初始石头,ans为删去石头的数量
for (int i = 1; i <= n; i++)
{
if (a[i] - a[j] < x) ans++;//距离达不到要求就一直往后找,同时去除石头
else j = i; //达到了就更是初始石头的位置
}
return ans;
}
//举例:
/*0 2 11 14 17 21 25 (0是起点,25是终点)
* 我们求的最小距离是4
* 0到2,距离小于4,删掉2; 2到11距离大于4,更新j到2的位置上;以此类推
*/
int main()
{
cin >> d >> n >> del;
for (int i = 1; i <= n; i++) cin >> a[i];
a[++n] = d;
sort(a + 1, a + n + 1);
int l = 1, r = d, mid;
int sum = 0;
while (l < r) //二分求最小的距离(二分模板)
{
mid = (l + r + 1) / 2;
if (find(mid) <= del) //如果当前求得的最小距离所需删除的石头数满足条件,则更新最小距离
{
sum = mid;
l = mid;
}
else r = mid - 1;
}
cout << sum << endl;
}
整数二分模板:
整数二分:
bool check(int x) {/* ... */} // 检查x是否满足某种性质
// 区间[l, r]被划分成[l, mid]和[mid + 1, r]时使用:
int bsearch_1(int l, int r)
{
while (l < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid; // check()判断mid是否满足性质
else l = mid + 1;
}
return l;
}
// 区间[l, r]被划分成[l, mid - 1]和[mid, r]时使用:
int bsearch_2(int l, int r)
{
while (l < r)
{
int mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
博客围绕奶牛跳房子游戏展开,游戏在有石头的河中进行。农夫约翰为增加奶牛跳跃最短距离,计划从N块石头中移除M块。需确定移除最优M块石头后,奶牛跳跃最短距离的最大值,涉及整数二分算法。
285

被折叠的 条评论
为什么被折叠?



