POJ 3258 River Hopscotch(二分)
文章地址:http://henuly.top/?p=764
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:
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 Input:
25 5 2
2
14
11
21
17
Sample Output:
4
题目链接
一头牛要通过跳跃踩石头过河,求去掉M个石头后石头之间最大的最小值。
通过二分最小值,统计达到此二分结果需要去掉最少几个石头,此结果和M进行比较,统计时计算距离,若大于二分结果则选择此石头,若小于不选择此石头。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
#define XDebug(x) cout<<#x<<"="<<x<<endl;
#define ArrayDebug(x,i) cout<<#x<<"["<<i<<"]="<<x[i]<<endl;
#define print(x) out(x);putchar('\n')
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
template <class T>
inline bool read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) {
return 0;
}
while (c != '-' && (c < '0' || c > '9')) {
c = getchar();
}
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') {
ret = ret * 10 + (c - '0');
}
ret *= sgn;
return 1;
}
template <class T>
inline void out(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) {
out(x / 10);
}
putchar(x % 10 + '0');
}
int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int L, N, M;
while (read(L) && read(N) && read(M)) {
vector<int> Stone(N + 1, 0);
for (int i = 1; i <= N; ++i) {
read(Stone[i]);
}
// 按照距离升序排序
sort(Stone.begin(), Stone.end());
// 对结果二分
int Left = 0, Right = L;
while (Left <= Right) {
int Mid = (Left + Right) / 2;
int Keep = 0, Cnt = 0;
for (int i = 1; i <= N; ++i) {
// 更新记录选择的石头位置
if (Stone[i] - Keep >= Mid) {
Keep = Stone[i];
}
// 统计未选择石头个数
else {
Cnt++;
}
}
if (L - Keep < Mid || Cnt > M) {
Right = Mid - 1;
}
else {
Left = Mid + 1;
}
}
print(Right);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("gedit out.txt");
#endif
return 0;
}
上一篇: 二分答案模板
下一篇: 牛客 矩阵 二维哈希+二分
推荐阅读
-
POJ 3258 River Hopscotch(二分)
-
二分 River Hopscotch (poj 3258)
-
3258 POJ River Hopscotch(二分)
-
poj 3258 River Hopscotch 【二分】
-
POJ 3258 River Hopscotch(二分)
-
POJ 3258 -- River Hopscotch(二分)
-
POJ3258 River Hopscotch(二分+贪心)
-
POJ3258 River Hopscotch (二分,最小值最大化)
-
Poj 3258 River Hopscotch 二分
-
POJ3258 River Hopscotch 二分 java