Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8377 | Accepted: 3602 |
Description
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
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
Sample Input
25 5 2 2 14 11 21 17
Sample Output
4
Hint
Source
解题报告:
这道题是对答案(距离)进行二分,然后再将这个距离带入到数组中,以贪心的思路从原点向后用这个距离去覆盖,如果覆盖的石头个数刚好满足题目中的m,那么就是符合情况的解,但是我们要让结果尽量大,所以就要在二分的时候让结果逼近右侧,有点相当与upper_bound。做这题的最大收获就是弄明白了二分靠左和靠右的两种情况。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int l, n, m;
int a[50005];
int check(int k) {
int cnt = 0, j, flag = 0;
for (int i = 0; i < n + 1; i = j) {
j = i + 1;
while(a[j] - a[i] < k) {
if(a[j] == l) {
return 1;
}
j ++;
cnt ++;
}
if (a[j] - a[i] == k) {
flag = 1;
}
}
if (m == cnt && flag == 1) {
return 0;
} else {
return cnt - m;
}
}
int binaSearch() {
int left = 0, right = 2 * l, mid;
// lower_bound
// while (left < right) {
// mid = (left + right) >> 1;
// if(check(mid) < 0) {
// left = mid + 1;
// }
// else {
// right = mid;
// }
// }
// return left;
//upper_bound
while (left < right) {
mid = (left + right) >> 1;
if(check(mid) > 0) {
right = mid;
} else {
left = mid + 1;
}
}
return left - 1;
}
int main() {
scanf("%d%d%d", &l, &n, &m);
a[0] = 0;
a[n+1] = l;
for (int i = 1; i <= n; i ++) {
scanf("%d", a + i);
}
sort(a, a + n + 1);
int ans = binaSearch();
cout << ans << endl;
return 0;
}