L距离中有n块石头,位置在d[i], 移走m块,使从起点0跳到终点l时,每次跳跃的最小距离最大,求这个最小距离
*解法:想到二分(想不到),对要求的结果进行二分,于是对最小距离二分==
#include <iostream> #include <cstdio> using namespace std; #define SS 50005 int d[SS]; int main() { int l, n, m, ans; scanf("%d %d %d", &l, &n, &m); for(int i = 0; i < n; i++) scanf("%d", &d[i]); d[n] = l; int left = 0, right = l, mid; //每次跳最少mid的距离,把结果二分 while(left < right) { mid = (left + right + 1) / 2; int now = 0, cnt = 0; //cnt 每次跳>=mid的话 要移走多少石头 for(int i = 0; i <= n; i++) { if(d[i] - now < mid) cnt++; else now = d[i]; } if(cnt <= m) left = mid; else right = mid - 1; } printf("%d", left); return 0; }