洛谷P3853 ——二分答案

题意:在长为L的线段上原有N个点,现要求插入K个点,使点之间距离最短。

解法:二分答案

#include<iostream>
using namespace std;
const int MAX=100000;
int l,n,k,a[MAX+5],r,L;
bool p(int x);
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int mid; cin>>L>>n>>k; for(int i=1;i<=n;i++) cin>>a[i]; a[0]=0; a[n+1]=L; r=L; l=0; while(l<r) { mid=l+r>>1; if(p(mid)) r=mid; else l=mid+1; } cout<<l; return 0; } bool p(int x) { int used=0,last; last=a[1]; for(int i=2;i<=n+1;i++) { if(a[i]-last<=x) last=a[i]; else { while(a[i]-last>x) { used++; last+=x; } last=max(last,a[i]); } } if(used>k) return 0; return 1; }

猜你喜欢

转载自www.cnblogs.com/nenT/p/11611952.html