题解:P14004 [eJOI 2025] Vacation

· · 题解

提供一个无聊的线性做法。

你发现这个的平移可以看作扩大,最后和所有区间大小取最小。

把所有的区间拆成 2n 个节点,然后答案区间可视为两个端点加一些其他的。

只要确定两个端点剩下的就是简单的问题了。贪心的选择向左向右扩张。

正向枚举共同区间的右端点、寻找最靠左的可行位置;反向枚举左端点、寻找最靠右的可行位置。

排序用基数排序就可以优化为 O(n)。

讲的不是很细,没理解的可以看代码。

:::success[code]{open}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mN = 5e5+5;
struct node {
    ll x, id, op;
};
node a[mN * 2];
pair<ll, ll> f[mN * 2], g[mN * 2];
inline void radix_sort(int n) {
    const int B = 10;
    const int BASE = 1 << B;
    const int MASK = BASE - 1;
    vector<node> tmp(n + 1);
    int cnt[BASE];

    for (int shift = 0; shift < 30; shift += B) {
        memset(cnt, 0, sizeof(cnt));
        for (int i = 1; i <= n; i++) {
            int digit = (a[i].x >> shift) & MASK;
            cnt[digit]++;
        }
        for (int i = 1; i < BASE; i++) {
            cnt[i] += cnt[i - 1];
        }
        for (int i = n; i >= 1; i--) {
            int digit = (a[i].x >> shift) & MASK;
            tmp[cnt[digit]--] = a[i];
        }
        for (int i = 1; i <= n; i++) {
            a[i] = tmp[i];
        }
    }
}
int plan_vacation(int N, std::vector<int> L, std::vector<int> R, long long K) {
    ll tans = 1000000001;
    for (int i = 0; i < N; i++) {
        a[i * 2 + 1] = {L[i], i + 1, 0};
        a[i * 2 + 2] = {R[i], i + 1, 1};
        tans = min(tans, R[i] - L[i] + 1ll);
    }
    radix_sort(2 * N);
    a[0].x = a[1].x - tans;
    a[2 * N + 1].x = a[2 * N].x + tans;
    ll bs = 0;
    for (ll i = 1; i <= 2 * N; i++) {
        f[i].first = f[i - 1].first + bs * (a[i].x - a[i - 1].x);
        if (a[i].op == 1)bs++;
        f[i].second = bs;
    }
    bs = 0;
    for (ll i = 2 * N; i >= 1; i--) {
        g[i].first = g[i + 1].first + bs * (a[i + 1].x - a[i].x);
        if (a[i].op == 0)bs++;
        g[i].second = bs;
    }
    ll ans = 0;
    for (ll i = 1, j = 1; i <= 2 * N && j <= 2 * N; i++) {
        if (a[i].x == a[i + 1].x)continue;
        while (j <= 2 * N && f[i].first + g[j].first > K)j++;
        if (j > 2 * N)break;
        ll nowans = a[i].x - a[j].x + 1, sy = K - f[i].first - g[j].first;
        if (f[i].second < g[j].second) {//f扩张便宜
            if (sy >= (a[i + 1].x - a[i].x) * f[i].second) {
                nowans += a[i + 1].x - a[i].x, sy -= (a[i + 1].x - a[i].x) * f[i].second;
            } else nowans += sy / f[i].second, sy %= f[i].second;
            if (sy >= (a[j].x - a[j - 1].x) * g[j].second) {
                nowans += a[j].x - a[j - 1].x, sy -= (a[j].x - a[j - 1].x) * g[j].second;
            } else nowans += sy / g[j].second, sy %= g[j].second;
        } else { //g扩张便宜
            if (sy >= (a[j].x - a[j - 1].x) * g[j].second) {
                nowans += a[j].x - a[j - 1].x, sy -= (a[j].x - a[j - 1].x) * g[j].second;
            } else nowans += sy / g[j].second, sy %= g[j].second;
            if (sy >= (a[i + 1].x - a[i].x) * f[i].second) {
                nowans += a[i + 1].x - a[i].x, sy -= (a[i + 1].x - a[i].x) * f[i].second;
            } else nowans += sy / f[i].second, sy %= f[i].second;
        }
        ans = max(ans, nowans);
    }
    ll ans2 = 0;
    for (ll i = 2 * N, j = 2 * N; i >= 1 && j >= 1; j--) {
        if (a[j].x == a[j - 1].x)continue;
        while (i >= 1 && f[i].first + g[j].first > K)i--;
        if (i < 1)break;
        ll nowans = a[i].x - a[j].x + 1, sy = K - f[i].first - g[j].first;
        if (f[i].second < g[j].second) {//f扩张便宜
            if (sy >= (a[i + 1].x - a[i].x) * f[i].second) {
                nowans += a[i + 1].x - a[i].x, sy -= (a[i + 1].x - a[i].x) * f[i].second;
            } else nowans += sy / f[i].second, sy %= f[i].second;
            if (sy >= (a[j].x - a[j - 1].x) * g[j].second) {
                nowans += a[j].x - a[j - 1].x, sy -= (a[j].x - a[j - 1].x) * g[j].second;
            } else nowans += sy / g[j].second, sy %= g[j].second;
        } else { //g扩张便宜
            if (sy >= (a[j].x - a[j - 1].x) * g[j].second) {
                nowans += a[j].x - a[j - 1].x, sy -= (a[j].x - a[j - 1].x) * g[j].second;
            } else nowans += sy / g[j].second, sy %= g[j].second;
            if (sy >= (a[i + 1].x - a[i].x) * f[i].second) {
                nowans += a[i + 1].x - a[i].x, sy -= (a[i + 1].x - a[i].x) * f[i].second;
            } else nowans += sy / f[i].second, sy %= f[i].second;
        }
        ans2 = max(ans2, nowans);
    }
    return min(max(ans, ans2), tans);
}
#undef ll

:::

注:本题解略使用 AI 润色。