Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
7
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
 
const double PI = 3.14159265358979;
const ll INF =  1e18 + 7;
const ll MOD = 1e9 + 7;
const ll nax = 1000005;
const int LOG = 25;


bool isPossibleToSeat(int dist, int k, vector<int> &a) {
    int cnt = 1, prev = a[0], n = a.size();
    for (int i = 1; i < n; i++) {
        if (a[i] - prev >= dist) {
            prev = a[i];
            cnt++;
        }
    }
    return cnt >= k;
}

void solve() {
    int n , k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());

    ll ans = 1, left = 1, right = a[n - 1] - a[0];
    while(left <= right) {
        ll mid = (left + right) / 2;
        if (isPossibleToSeat(mid, k, a)) {
            ans = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    cout << ans << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t; cin >> t; while(t--)
    solve();
    return 0;
}
Leave a Comment