fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. vector<int> a;
  25.  
  26. bool isPossible(int n, int k, vector<int>& a, int mid){
  27. int cows = 1;
  28. for(int i=0; i<n; ){
  29. int j = i+1;
  30. while(j<n && a[j] - a[i] < mid) j++;
  31. if(j==n) break;
  32. cows++;
  33. i = j;
  34. }
  35. return cows>=k;
  36. }
  37.  
  38. int consistency(int n, int k, vector<int>& a){
  39. sort(begin(a), end(a));
  40.  
  41. int s = 0, e = INF;
  42. int ans = 0;
  43. while(s<=e){
  44. int mid = s + (e-s)/2;
  45. if(isPossible(n, k, a, mid)){
  46. ans = max(ans, mid);
  47. s = mid+1;
  48. }
  49. else{
  50. e = mid-1;
  51. }
  52.  
  53. }
  54.  
  55. return ans;
  56.  
  57. }
  58.  
  59.  
  60. void solve() {
  61.  
  62. int n, k;
  63. cin>>n >> k;
  64. a.resize(n);
  65.  
  66. for(int i=0; i<n; i++) cin >> a[i];
  67. cout << consistency(n, k, a) << endl;
  68.  
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. int32_t main() {
  77. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  78.  
  79. int t = 1;
  80. // cin >> t;
  81. while (t--) {
  82. solve();
  83. }
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0s 5316KB
stdin
5 3
1 2 8 4 9
stdout
3