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. for(int i=1;i<=n;i++)
  15. {
  16. if(x[i]-x[pos]>=d)
  17. {
  18. pos=i;
  19. cnt++;
  20. }
  21. }
  22. return cnt>=m;
  23. }
  24.  
  25. int main()
  26. {
  27. cin>>n>>m;
  28. for(int i=1;i<=n;i++)cin>>x[i];
  29. sort(x+1,x+n+1);
  30.  
  31. int l=0,r=x[n]-x[1];
  32. while(l<r)
  33. {
  34. int mid=(l+r+1)/2;
  35. if(check(mid))
  36. {
  37. l=mid;
  38. }
  39. else
  40. {
  41. r=mid-1;
  42. }
  43. }
  44.  
  45. cout<<l<<endl;
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 5308KB
stdin
5 3
1
2
8
4
9
stdout
3