跳到正文

题解

题解:P14004 [eJOI 2025] Vacation

masonxiong约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年9月17日
收录
2026年7月2日
更新
2026年11月11日
篇幅
约 7 分钟
获赞
1
题目
P14004

版本

  1. v1当前

给定 nn 个区间 [li,ri][l_i,r_i],你可以将它们平移任意整数长度,要求所有平移长度之和 ∑di\sum d_i 不超过 kk,求全体区间交集长度最大值。

答案具有单调性,于是我们考虑二分这个交集长度最大值。现在我们要做的是:对于一个 ww,判断是否可以将区间按要求平移使得其交集长度不小于 ww。

这个长度为 ww 的交集肯定也是一个区间,不妨假设其左端点是 pp,那这个区间就是 [p,p+w−1][p,p+w-1]。现在我们需要将它左边的区间向右平移、将它右边的区间向左平移以覆盖这个区间。

我们知道,对于一个区间 [li,ri][l_i,r_i],如果你想平移它以覆盖 [p,p+w−1][p,p+w-1],那就需要满足 li+di≤p,ri+di≥p+w−1l_i+d_i\le p,r_i+d_i\ge p+w-1 也就是 di∈[p+w−1−ri,p−li]d_i\in[p+w-1-r_i,p-l_i],所需要的最小代价是 00 到 [pw−1−ri,p−li][p_w-1-r_i,p-l_i] 的最短距离,也就是 pp 到 [li,ri−w+1][l_i,r_i-w+1] 的最短距离,记这个距离为 cic_i。

记 f(p)=∑cif(p)=\sum c_i,那么 f(p)f(p) 就是钦定答案区间左端点为 pp 的最小总代价,那么你的判断条件等价于 fmin⁡≤kf_{\min}\le k。f(p)f(p) 在对 l,rl,r 排序并求前缀和后是可以使用二分 O(log⁡n)O(\log n) 计算的。而且 f(p)f(p) 的图像是一个先递减再递增的折线,于是你可以二分查询其最小值。具体地,二分出让折线斜率负变正的拐点 pp,计算 pp 和 p−1p-1 的 ff 值取最小即可。

时间复杂度为 O(nlog⁡n+log⁡w⋅log⁡n)O(n\log n+\log w\cdot\log n),nlog⁡nn\log n 由预处理的排序部分提供,log⁡w\log w 由外层二分答案提供,log⁡n\log n 由内层最小值二分提供。

代码
C++
int plan_vacation(int N, std::vector<int> L, std::vector<int> R, long long K) {
    const std::uint64_t friendCount = std::uint64_t(N), constraint = std::uint64_t(K);
    std::vector<std::uint64_t> leftEndpoint(L.begin(), L.end()), rightEndpoint(R.begin(), R.end()), leftPrefixSum(friendCount), rightPrefixSum(friendCount);
    const std::uint64_t minLength = [&]() { std::uint64_t result = std::uint64_t(-1); for (std::uint64_t i = friendCount; i--; result = std::min(result, rightEndpoint[i] - leftEndpoint[i] + 1)); return result; }();
    std::uint64_t answerRangeLeft = 0, answerRangeRight = minLength + 1; 
    std::ranges::sort(leftEndpoint), std::ranges::sort(rightEndpoint), std::partial_sum(leftEndpoint.begin(), leftEndpoint.end(), leftPrefixSum.begin()), std::partial_sum(rightEndpoint.begin(), rightEndpoint.end(), rightPrefixSum.begin());
    while (answerRangeLeft < answerRangeRight) {
        const std::uint64_t possibleAnswer = (answerRangeLeft + answerRangeRight + 1) >> 1;
        [&]() {
            if (possibleAnswer > minLength)
                return false;
            const std::uint64_t shift = possibleAnswer - 1, minimumLeft = leftEndpoint.front(), maximumRight = rightEndpoint.back() - shift;
            if (minimumLeft > maximumRight)
                return false;
            auto cost = [&](std::uint64_t position) -> std::uint64_t {
                const std::uint64_t rightIndex = std::uint64_t(std::lower_bound(rightEndpoint.begin(), rightEndpoint.end(), position + shift) - rightEndpoint.begin());
                const std::uint64_t leftCost = position * rightIndex - ((rightIndex ? rightPrefixSum[rightIndex - 1] : 0) - shift * rightIndex);
                const std::uint64_t leftIndex = std::uint64_t(std::upper_bound(leftEndpoint.begin(), leftEndpoint.end(), position) - leftEndpoint.begin());
                const std::uint64_t rightCost = (leftPrefixSum.back() - (leftIndex ? leftPrefixSum[leftIndex - 1] : 0)) - position * (friendCount - leftIndex);
                return leftCost + rightCost;
            };
            std::uint64_t positionRangeLeft = minimumLeft, positionRangeRight = maximumRight;
            while (positionRangeLeft < positionRangeRight) {
                const std::uint64_t possiblePosition = (positionRangeLeft + positionRangeRight) >> 1;
                std::lower_bound(rightEndpoint.begin(), rightEndpoint.end(), possiblePosition + shift) - rightEndpoint.begin() < leftEndpoint.end() - std::upper_bound(leftEndpoint.begin(), leftEndpoint.end(), possiblePosition) ? positionRangeLeft = possiblePosition + 1 : positionRangeRight = possiblePosition;
            }
            return (positionRangeLeft > minimumLeft ? std::min(cost(positionRangeLeft), cost(positionRangeLeft - 1)) : cost(positionRangeLeft)) <= constraint;
        }() ? answerRangeLeft = possibleAnswer : answerRangeRight = possibleAnswer - 1;
    }
    return int(answerRangeLeft);
}
原文
存档评论3 条抓取于