제출 #1345951

#제출 시각아이디문제언어결과실행 시간메모리
1345951thaibaotran555Watching (JOI13_watching)C++20
100 / 100
95 ms31696 KiB
///TRAN THAI BAO :3

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

#define maxN 2007
int n, p, q;
vector<long long>a;

long long dp[maxN][maxN]; //q op req to reach the end after spending j op p and at pos i

int nxtP[maxN];
int nxtQ[maxN];

bool check(long long w)
{
    if(p + q >= n)
        return true;
    int j = 0;
    for(int i = 0; i < n; i++)
    {
        while(j < n && a[j] - a[i] < w)
            j++;
        nxtP[i] = j;
    }
    j = 0;
    for(int i = 0; i < n; i++)
    {
        while(j < n && a[j] - a[i] < w*2)
            j++;
        nxtQ[i] = j;
    }
    for(int i = 0; i <= p; i++)
        dp[n][i] = 0;
    for(int i = n-1; i >= 0; i--)
    {
        for(int j = 0; j <= p; j++)
        {
            dp[i][j] = dp[nxtQ[i]][j] + 1;
            if(j > 0)
                dp[i][j] = min(dp[i][j], dp[nxtP[i]][j-1]);
        }
    }
    return dp[0][p] <= q;
}

void solve()
{
    cin >> n >> p >> q;
    p = min(n, p);
    q = min(n, q);
    for(int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        a.push_back(x);
    }
    sort(a.begin(), a.end());
    int lo = 1, hi = 1000000000, mid, ans = 1000000000;
    while(lo <= hi)
    {
        mid = (lo+hi)/2;
        if(check(mid))
        {
            ans = min(ans, mid);
            hi = mid-1;
        }
        else lo = mid+1;
    }
    cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...