Submission #1051803

#TimeUsernameProblemLanguageResultExecution timeMemory
1051803fryingducWatching (JOI13_watching)C++17
100 / 100
340 ms30520 KiB
#include "bits/stdc++.h"
using namespace std;

#define int long long

const int maxn = 2005;
int n, a[maxn], p, q;

bool check(int mid) {
    vector<vector<int>> f(n + 1, vector<int>(p + 2, 1e9));
    for(int i = 0; i <= p + 1; ++i) {
        f[0][i] = 0;
    }
    int pl = 0, pr = 0;
    for(int i = 1; i <= n; ++i) {
        while(pl < i and a[i] - mid + 1 > a[pl]) ++pl;
        while(pr < i and a[i] - mid * 2 + 1 > a[pr]) ++pr;
        if(a[pl] >= a[i] - mid + 1 and pl) --pl;
        if(a[pr] >= a[i] - mid * 2 + 1 and pr) --pr;
        
        for(int j = 0; j <= p; ++j) {
            f[i][j] = f[pr][j] + 1;
            if(j) f[i][j] = min(f[i][j], f[pl][j - 1]);
        }
    }
    
    int ans = 1e9;
    for(int i = 0; i <= p; ++i) {
        ans = min(ans, f[n][i]);
    }
    return ans <= q;
}
void solve() {
    cin >> n >> p >> q;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    if(p + q >= n) {
        cout << 1 << '\n';
        return;
    }
    sort(a + 1, a + n + 1);
    int l = 0, r = 1e9, res = -1;
    while(l <= r) {
        int mid = (l + r) / 2;
        if(check(mid)) {
            res = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
//    cerr << check(9);
    cout << res;
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...