跳到正文

题解

题解:P10093 [ROIR 2022] 礼物 (Day 2)

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

文章信息

发布
2025年5月24日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 7 分钟
获赞
2
题目
P10093

版本

  1. v1当前

神秘乱搞申请出战!!1

省流:一点都不对的乱搞。

首先定义下文的答案区间为最后你选择的那个区间。

然后你要会算固定区间的答案。具体的,就是你开一个堆维护前 kk 大就可以算了。

就你首先算一下样例,发现了一个非常厉害的事情,就是样例的答案区间其实就是原序列的最大子段和区间!!

但是你写一发发现这是错的。

然后你考虑对于每个点求出以这个点结尾的所有子段里面和最大的那个,这个你直接按照最大子段和求法 dp 一下就可以做。

然后你考虑从这个里面取子段和前 100100 大的子段拿出来算答案,通过直觉感受一下答案区间距离这个子段不是很多,所以你对每个提出来的子段左右扰动 33 个点去算答案。

然后你就可以 4747 分了,并且只错了很少的点。

那么你观察到前 100100 大的子段里其实有很多子段是无效的,所以你考虑再对于每个点向后扫 1010 个点计算答案,最后将两个答案取较大值即可。

然后这个很没道理的东西就过了。

C++
#include <bits/stdc++.h>

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using i128 = __int128;
using u128 = unsigned __int128;

#define int i64

constexpr int N = 2e5 + 5;

int n, k, l, r, a[N], dp[N], id[N], s[N];
std::tuple<int, int, int> nw[N];

inline int calc(int l, int r) {
    if (!l || r > n) return 0;
    std::priority_queue<int, std::vector<int>, std::greater<int> > q;

    int s = 0, ss = 0;

    for (int i = l; i <= r; i++) {
        if (q.size() < k) {
            q.push(a[i]);
            s += a[i];
        } else {
            int xx = q.top();
            if (xx < a[i]) {
                s -= xx;
                q.pop();
                q.push(a[i]);
                s += a[i];
            }
        }
        ss += a[i];
    }

    return ss - s;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);

    std::cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        std::cin >> a[i];
        id[i] = i + 1;
        s[i] = a[i];
    }

    for (int i = 1; i <= n; i++) {
        if (dp[i - 1] > 0) {
            id[i] = id[i - 1];
            dp[i] = dp[i - 1];
        } else {
            id[i] = i;
            dp[i] = 0;
        }
        dp[i] += a[i];
        s[i] = s[i - 1] + a[i];
    }
    
    if (!k) {
        std::cout << (*std::max_element(dp + 1, dp + n + 1)) << '\n';
        return 0;
    } 

    // int maxn = -1e9;

    for (int i = 1; i <= n; i++) {
        // if (maxn < dp[i] && i - id[i] + 1 >= k) {
        //     maxn = dp[i];
        //     l = id[i];
        //     r = i;
        // }
        nw[i] = std::make_tuple(dp[i], id[i], i);
    }

    std::sort(nw + 1, nw + n + 1, std::greater<std::tuple<int, int, int>>());

    int ct = 0, i = 1;
    int maxn = 0;
    while (i <= n && ct <= 100) {
        while (i <= n && std::get<2> (nw[i]) - std::get<1> (nw[i]) + 1 < k) i++;
        if (i > n) break;
        maxn = std::max(maxn, calc(std::get<1> (nw[i]), std::get<2> (nw[i])));
        for (int kk = 1; kk <= 3; kk++) {
            maxn = std::max(maxn, calc(std::get<1> (nw[i]) - kk, std::get<2> (nw[i])));
            maxn = std::max(maxn, calc(std::get<1> (nw[i]), std::get<2> (nw[i]) + kk));
            maxn = std::max(maxn, calc(std::get<1> (nw[i]) - kk, std::get<2> (nw[i]) + kk));
        }
        ct++;
    }
    int ans = maxn;
    for (int i = 1; i <= n; i++) {
        std::priority_queue<int, std::vector<int>, std::greater<int> > q;

        int rs = 0;

        for (int j = i; j <= std::min(n, i + 10); j++) {
            if (q.size() < k) {
                q.push(a[j]);
                rs += a[j];
            } else {
                int xx = q.top();
                if (xx < a[j]) {
                    rs -= xx;
                    q.pop();
                    q.push(a[j]);
                    rs += a[j];
                }
            }
            if (q.size() >= k) ans = std::max(ans, s[j] - s[i - 1] - rs);
        }
    }
    maxn = ans;

    std::cout << maxn;

    return 0;
}
原文
存档评论2 条抓取于