fork download
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. const int N=1e5+10;
  6. typedef long long LL;
  7. LL x[N];
  8. LL n,m;
  9.  
  10. bool check(int d)
  11. {
  12. int pos=1;
  13. int cnt=1;
  14. while(cnt<m)
  15. {
  16. auto it=lower_bound(x+pos+1,x+1+n,x[pos]+d);
  17. if(it==x+1+n)return false;
  18. pos=it-x;
  19. cnt++;
  20. }
  21. return cnt>=m;
  22. }
  23.  
  24. int main()
  25. {
  26. cin>>n>>m;
  27. for(int i=1;i<=n;i++)cin>>x[i];
  28. sort(x+1,x+n+1);
  29.  
  30. int l=0,r=x[n]-x[1];
  31. while(l<r)
  32. {
  33. int mid=(l+r+1)/2;
  34. if(check(mid))
  35. {
  36. l=mid;
  37. }
  38. else
  39. {
  40. r=mid-1;
  41. }
  42. }
  43.  
  44. cout<<l<<endl;
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0.01s 5316KB
stdin
5 3
1
2
8
4
9
stdout
3