Advertisement
ahamed210

Agg_cow

Nov 6th, 2021
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef int l;
  4. typedef long long int ll;
  5. #define loop(a, b) for(int i = a; i < b; i++)
  6.  
  7. void run(){
  8.     ios::sync_with_stdio(false);
  9.     cin.tie(NULL);
  10.     cout.tie(NULL);
  11. #ifndef ONLINE_JUDGE
  12.     freopen("input.txt", "r", stdin);
  13.     freopen("output7.txt", "w", stdout);
  14. #else
  15. #endif
  16. }
  17.  
  18. bool check(ll ara[], l n, l m, ll mid){
  19.     m--;
  20.     l current = 0;
  21.     l j = 1;
  22.     while(j < n){
  23.         if(abs(ara[current] - ara[j]) >= mid){
  24.             current = j;
  25.             m--;
  26.         }
  27.         if(m <= 0) return true;
  28.         j++;
  29.     }
  30.     return false;
  31. }
  32.  
  33. int main()
  34. {
  35.     ios::sync_with_stdio(false);
  36.     cin.tie(NULL);
  37.     cout.tie(NULL);
  38.     //run();
  39.     l t;cin>>t;
  40.     while(t--){
  41.         l n, m;cin>>n>>m;
  42.         ll ara[n];loop(0, n) cin>>ara[i];
  43.         sort(ara, ara+n);
  44.         ll left = 2, right = abs(ara[n-1] - ara[0]);
  45.         while(left <= right){
  46.             l mid = left + ((right - left) >> 1);
  47.             if(check(ara, n, m, mid)) left = mid + 1;
  48.             else right = mid - 1;
  49.         }
  50.         cout << right << endl;
  51.     }
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement