#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> using namespace std; int n,c; int a[100000+100]; int solve(int m) { int ret=1; int i,j,k; int l=0,r; for(i=1;i<n;i++) { if(a[i]-a[l]>=m) { ret++; l=i; } } return ret; } /* bool solve(int x) { int cnt = 1, tmp = a[0]; for(int i = 1; i < n; i++) { if(a[i] - tmp >= x) { cnt++; tmp = a[i]; if(cnt >= c) //可以放下C头牛 return true; } } return false; } */ int erfen(int l,int r) { //printf("%d......\n",solve(2)); while(l<=r) { int m=(l+r)>>1; //printf("%d %d %d\n",l,m,r); //printf("sol %d %d\n",m,solve(m)); if(solve(m)>=c) //if(solve(m)) { l=m+1; } else //if(solve(m)<c) { r=m-1; } //printf("%d %d..\n",l,r); } //if(solve(r)==c) return r; return l; } int main() { int i,j,k; while(scanf("%d%d",&n,&c)!=EOF) { for(i=0;i<n;i++) { scanf("%d",&a[i]); } sort(a,a+n); int ans; ans=erfen(0,a[n-1]-a[0]); printf("%d\n",ans-1); } return 0; }
时间: 2024-10-13 13:58:24