제출 #1356458

#제출 시각아이디문제언어결과실행 시간메모리
1356458Aliyyiakbar구경하기 (JOI13_watching)C++20
50 / 100
1098 ms86556 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#define int long long
using namespace std;
using namespace __gnu_pbds;

const int sz = 3e5 + 9;
int a[sz];

int n, p, q;

int raiz(int idx, int p1, int q1)
{
    size_t res = 0;
    res ^= idx + 0x9e3779b9 + (res << 6) + (res >> 2);
    res ^= p1 + 0x9e3779b9 + (res << 6) + (res >> 2);
    res ^= q1 + 0x9e3779b9 + (res << 6) + (res >> 2);
    return res;
}

gp_hash_table<int, bool> cache;
bool dfs(int idx, int p1, int q1, int w)
{
    if (idx >= n)
    {
        return 1;
    }
    if (p1 + q1 == 0)
    {
        return 0;
    }
    int key = raiz(idx, p1, q1);
    if (cache.find(key) != cache.end())
    {
        return cache[key];
    }
    bool flag = 0;
    if (p1 > 0)
    {
        int j = upper_bound(a, a + n, a[idx] + w - 1) - a;
        flag |= dfs(j, p1 - 1, q1, w);
    }
    if (q1 > 0)
    {
        int j = upper_bound(a, a + n, a[idx] + 2 * w - 1) - a;
        flag |= dfs(j, p1, q1 - 1, w);
    }
    return cache[key] = flag;
}

bool f(int w)
{
    cache.clear();
    return dfs(0, p, q, w);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    cin >> n >> p >> q;
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }
    sort(a, a + n);
    int l = 1, r = 1e9, best = r, mid;
    while (l <= r)
    {
        mid = (l + r) >> 1;
        if (f(mid))
        {
            best = mid;
            r = mid - 1;
        }
        else
        {
            l = mid + 1;
        }
    }
    cout << best << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...