题目:https://www.luogu.org/problemnew/show/P2678
不要以为是做过的那个题。想一想就有了。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=5e4+5; int n,m,L,p[N],d[N],l=0x3f3f3f3f,r,ans,ynt; int rdn() { int ret=0;char ch=getchar(); while(ch>'9'||ch<'0') ch=getchar(); while(ch>='0'&&ch<='9') ret=(ret<<3)+(ret<<1)+ch-'0',ch=getchar(); return ret; } int main() { L=rdn();n=rdn();m=rdn(); m=n-m+1;//至少m段 for(int i=1;i<=n;i++) { p[i]=rdn(); d[i]=p[i]-p[i-1];l=min(l,d[i]); } d[n+1]=L-p[n]; l=min(l,d[n+1]); r=L;//别忘了与d[n+1]取min while(l<=r) { int mid=l+r>>1,s=0,cnt=0; for(int i=1;i<=n+1;i++) { if(s+d[i]>=mid)cnt++,s=0; else s+=d[i]; }//有s剩余也不管 if(cnt>=m) ans=mid,l=mid+1; else r=mid-1; } printf("%d\n",ans); return 0; }