一道简单的二分。
#include <string.h> #include <stdio.h> #include <algorithm> #define MAXN 500005 int l,n,m; int d[MAXN]; int cant(int x){ int now=0,ans=0; for(int i=1;i<=n;i++){ if(d[i]-d[i-1]>x)return 1; if(d[i+1]-d[now]>x)now=i,ans++; } //最后一步要跳到终点 if(now<n)ans++; return ans>m; } int main(){ freopen("test.in","r",stdin); while(scanf("%d%d%d",&l,&n,&m)!=EOF){ for(int i=1;i<=n;i++)scanf("%d",&d[i]); d[0]=0,d[++n]=l; std::sort(d,d+n+1); int low=0,high=l,mid; for(;;){ mid=(low+high)>>1; if(mid==low)break; if(cant(mid))low=mid; else high=mid; } printf("%d\n",mid+1); } return 0; }