File size: 1,086 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
   miscellaneous > binary search > on answer
   difficulty: medium
   date: 03/Apr/2020
   problem: given an array A of size N (2 <= N <= 10^5), print the largest minimum distance between any two of C (C <= N) elements choosen any
   by: @brpapa
*/
#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;

int N, C;
vector<ll> A; 

// consigo escolher C elementos de A mantendo uma distância mínima entre eles de ans?
bool can(ll ans) {
   int k = 0; // A[k] é o último elemento escolhido
   int c = 1;

   for (int i = 1; i < N && c < C; i++)
      if (A[i]-A[k] >= ans) {
         c++;
         k = i;
      }
   return c >= C;
}

int main() {
   int T; cin >> T;
   while (T--) {
      cin >> N >> C;
      A.resize(N); for (ll &a: A) cin >> a;
      sort(A.begin(), A.end());

      ll low = 0, high = A[N-1]-A[0];
      ll ans;

      while (low <= high) {
         ll mid = (low+high)/2;

         if (can(mid)) low = mid+1, ans = mid;
         else          high = mid-1;
      }
      cout << ans << endl;
   }
   return 0;
}