Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.1 kB
4
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;


void solve() {

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    auto check = [&](int &D) {
        ll points = 0;
        for (int i = 1; i < n; i++) {
            int dist = a[i] - a[i - 1];
            points += (dist + D - 1) / D;
            points -= 1;
        }
        return points <= k;
    };

    int ans = a[1] - a[0], left = 1, right = a[n - 1] - a[0];
    while(left <= right) {
        int mid = (left + right) / 2;
        if (check(mid)) {
            ans = mid;
            right = mid - 1;
        } else {
            left = 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