2072 分配房间
二分
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 #define maxn 1000000 5 #define LL long long 6 7 LL n,m,num[maxn],ans; 8 9 inline void read(LL &now) 10 { 11 char ch=getchar(); now=0; 12 while(ch>'9'||ch<'0') ch=getchar(); 13 while(ch>='0'&&ch<='9') now=now*10+ch-'0',ch=getchar(); 14 } 15 16 bool check(LL x) 17 { 18 int t=1,tot=1; 19 for(int i=2;i<=n;i++) 20 if(num[i]-num[t]>=x) tot++,t=i; 21 return tot>=m?true:false; 22 } 23 24 int main() 25 { 26 read(n); read(m); 27 LL l=0,r=0; 28 for(int i=1;i<=n;i++) read(num[i]); 29 sort(num+1,num+n+1); 30 r=num[n]-num[1]; 31 while(l<r) 32 { 33 LL mid=(l+r)>>1; 34 if(check(mid)) ans=mid,l=mid+1; 35 else r=mid; 36 } 37 printf("%lld",ans); 38 return 0; 39 }