제출 #1327064

#제출 시각아이디문제언어결과실행 시간메모리
1327064hoangtien69구경하기 (JOI13_watching)C++20
0 / 100
36 ms9972 KiB
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2005;
const int MAXX = 1e5 + 5;

int n;
int a[MAXN];
int p, q;
int cost[MAXN][MAXN];
int dp[MAXN][MAXX];

int tinh(int w)
{
    for (int j = 0; j <= p; j++)
    {
        dp[0][j] = 0;
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= p; j++)
        {
            dp[i][j] = INT_MAX;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        int pos1 = i;
        while (pos1 > 1 and a[i] - a[pos1 - 1] + 1 <= w)
        {
            pos1--;
        }
        int pos2 = pos1;
        while(pos2 > 1 and a[i] - a[pos2 - 1] + 1 <= 2 * w)
        {
            pos2--;
        }
        for (int j = 0; j <= p; j++)
        {
            if (dp[pos2 - 1][j] != INT_MAX)
            {
               dp[i][j] = min(dp[i][j], dp[pos2 - 1][j] + 1);
            }
            if (j > 0 and dp[pos1 - 1][j - 1] != INT_MAX)
            {
                dp[i][j] = min(dp[i][j], dp[pos1 - 1][j - 1]);
            }
        }
    }
    int res = INT_MAX;
    for (int i = 0; i <= p; i++)
    {
        res = min(res, dp[n][i]);
    }
    return res;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

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