Submission #1281068

#TimeUsernameProblemLanguageResultExecution timeMemory
1281068nathlol2Watching (JOI13_watching)C++20
100 / 100
304 ms16192 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
int n, p, q;
vector<int> a, s(N), l(N);
int dp[N][N];
bool ok(int x){
    // cout << x << '\n';
    for(int i = 0;i<n;i++){
        s[i] = upper_bound(a.begin(), a.end(), a[i] + x - 1) - a.begin();
        l[i] = upper_bound(a.begin(), a.end(), a[i] + 2 * x - 1) - a.begin();
    }
    // for(int i = 0;i<n;i++){
    //     cout << s[i] << ' ';
    // }
    // cout << '\n';
    // for(int i = 0;i<n;i++){
    //     cout << l[i] << ' ';
    // }
    // cout << '\n';
    for(int i = 0;i<N;i++){
        for(int j = 0;j<N;j++){
            dp[i][j] = 1e9;
        }
    }
    dp[0][0] = 0;
    for(int i = 0;i<=p;i++){
        for(int j = 0;j<n;j++){
            dp[i + 1][s[j]] = min(dp[i + 1][s[j]], dp[i][j]);
            dp[i][l[j]] = min(dp[i][l[j]], dp[i][j] + 1);
        }
    }
    // for(int i = 0;i<=p;i++){
    //     for(int j = 0;j<=n;j++){
    //         cout << dp[i][j] << ' ';
    //     }
    //     cout << '\n';
    // }
    for(int i = 0;i<=p;i++){
        if(dp[i][n] <= q) return 1;
    }
    return 0;
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> p >> q;
    a.resize(n);
    for(int i = 0;i<n;i++){
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    if(p + q >= n) return cout << 1, 0;
    int l = 1, r = 1e9, ans = -1;
    while(l <= r){
        int md = (l + r) / 2;
        if(ok(md)){
            ans = md;
            r = md - 1;
        }else{
            l = md + 1;
        }
    }
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...