洛谷 P1316 丢瓶盖
P1316 丢瓶盖
二分答案
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,pg[100001]; 4 5 char ch; 6 inline void read(int &now) 7 { 8 int f=1; ch=getchar(); now=0; 9 while(ch>'9'||ch<'0') {if(ch=='-') f*=-1; ch=getchar();} 10 while(ch>='0'&&ch<='9') now=now*10+ch-'0',ch=getchar(); 11 now*=f; 12 } 13 14 bool check(int now) 15 { 16 int pre=1,tot=1; 17 for(int i=2;i<=n;i++) 18 if(pg[i]-pg[pre]>=now) 19 { 20 tot++; 21 pre=i; 22 } 23 if(tot<m) return false; 24 return true; 25 } 26 27 int main() 28 { 29 read(n); read(m); 30 for(int i=1;i<=n;i++) read(pg[i]); 31 sort(pg+1,pg+n+1); 32 int l=1,r=pg[n]-pg[1],mid; 33 while(r>=l) 34 { 35 mid=(l+r)/2; 36 if(check(mid)) l=mid+1; 37 else r=mid-1; 38 } 39 cout<<l-1; 40 return 0; 41 }