'commit'
This commit is contained in:
42
TangDou/LuoGuBook/P2678.cpp
Normal file
42
TangDou/LuoGuBook/P2678.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
|
||||
//典型的二分答案题, 答案为最短(可以理解为最小)的跳跃距离最大值
|
||||
|
||||
int L;
|
||||
int n, m;
|
||||
const int N = 50010;
|
||||
int a[N];
|
||||
|
||||
// 二分的检查函数,如果跳跃长度是mid,
|
||||
// 是不是可以达到减少m个石头的目标
|
||||
// 因为每个石头都是可能被移除掉的,所以,当前的石头不能和上一个石头进行判定(因为上一个石头可能已经被移除了),需要有一个变量,记录最后一个活着的石头是哪一个。
|
||||
bool check(LL mid) {
|
||||
int t = 0; //需要移除石头的数量
|
||||
int now = 0; //最后一个保留的石头索引(这个变量用的漂亮)
|
||||
for (int i = 1; i <= n; i++)
|
||||
if (a[i] - a[now] < mid) t++; //移除数量+1
|
||||
else now = i;
|
||||
return t <= m;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// L:起点到终点的距离
|
||||
// n:起点和终点之间的岩石数
|
||||
// m:组委会至多移走的岩石数
|
||||
cin >> L >> n >> m;
|
||||
for (int i = 1; i <= n; i++) cin >> a[i];
|
||||
//表示第i块岩石与起点的距离,这些岩石按与起点距离从小到大的顺序给出,且不会有两个岩石出现在同一个位置
|
||||
|
||||
LL l = 0, r = L;
|
||||
while (l < r) {
|
||||
int mid = l + r + 1 >> 1;
|
||||
if (check(mid)) l = mid;
|
||||
else r = mid - 1;
|
||||
}
|
||||
//输出
|
||||
cout << l << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user