该题目采用二分的思想:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 50000 + 10;
int l, n, m;
int dis[N];
int main(){
while(cin >> l >> n >> m){
dis[0] = 0;
dis[n + 1] = l;
int low = l;
int high = l;
for(int i = 1; i <= n + 1; i ++ ){
if(i <= n) cin >> dis[i];
if(low > dis[i] - dis[i - 1]) low = dis[i] - dis[i - 1];
}
sort(dis, dis + n + 1);
while(low <= high){
int mid = low + high >> 1;
int num = 0;
int sum = 0;
for(int i = 1; i <= n + 1; i ++ ){
sum += dis[i] - dis[i - 1];
if(sum <= mid) num ++;
else sum = 0;
}
if(num <= m) low = mid + 1;
else high = mid - 1;
}
cout << low << endl;
}
return 0;
}
|