POJ 2456 二分 贪心
拿来练了一下二分,对于本菜鸡而言,光看不写肯定是学不会的。
顺便测试了一下内联函数
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1e5+5;
int a[N],n,c,ans=-1;
inline bool check(int d) {
int cnt=1,tmp=0;
for(int i=0; i<n; i++) {
if(a[i]-a[tmp]>=d)cnt++,tmp=i;
if(c==cnt) return 1;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
while(cin>>n>>c) {
for(int i=0; i<n; i++) cin>>a[i];
sort(a,a+n);
int R=(a[n-1]-a[0])/(c-1);
int L=1,p;
while(L<=R) {
p=L+((R-L)>>1);
if(check(p)) ans=p,L=p+1;
else R=p-1;
}
cout<<ans<<endl;
}
return 0;
}