二分答案
怎么加路标是关键
一开始我想的是二分加,每次加中间,那边距离多出来就往哪边去加
但实际上压根不能这么算,很奇怪,为啥呢?感觉应该是会浪费的缘故,就往中间插实际上不如隔着x顺序插好,被二分和遍历背刺的第二次,有时候二分做不到优化甚至压根不正确,遍历才是真爱啊
// Problem: P3853 [TJOI2007] 路标设置
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3853
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// Date: 2024-03-26 21:01:02
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
#define endl '\n'
#define int int64_t
#define ld long double
using namespace std;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };
const int NC = 2e3 + 10;
char ches[NC][NC];
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int a[N], b[N], n, m, k;
bool check(int x) {
//需要几个路标
int tot = 0;
for (int i = 2; i <= n; ++i) {
if (a[i] - a[i - 1] <= x) continue;
int cnt = 0;
int sub = a[i] - a[i - 1];
tot += (sub - 1)/x;
//cout << "tot = " << tot << endl;
if (tot > k) return false;
}
return true;
}
void solve() {
cin >> m >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
int l = 1, r = m, ans = m;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}