跳到正文

题解

题解:P10381 「HOI R1」杂赛选比

__xsy2013__约 6 分钟
原文
文章信息与版本

文章信息

发布
2024年4月28日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 6 分钟
题目
P10381

版本

  1. v1当前

前言

收集了部分分大全,有其它部分分私信我,我会加上的。

乱搞哥

5pts5pts

我们可以乱搞拿到,其他人是怎么搞的我也不知道。

C++
# include <bits/stdc++.h>
using namespace std;
int main() {
    int T; cin >> T;
    while (T--) {
        int n; cin >> n;
        vector<long long> a(n + 1);
        for (int i = 1; i <= n; ++i) cin >> a[i];
        vector<long long> dp(n + 1, 0);
        for (int i = n; i >= 1; --i) {
            if (i + a[i] <= n) dp[i] = max(dp[i], dp[i + a[i]]);
            dp[i] += a[i];
            for (int j = i + 1; j <= i + a[i] && j <= n; ++j) {
                if (a[j] <= 0) dp[i] = max(dp[i], dp[j]);
            }
        }
        cout << dp[1] << endl;
    }
    return 0;
}

评测记录

15pts15pts

使我大脑旋转。

C++
#include <iostream>
#include <vector>
using namespace std;

#define int long long
const int MAX_SIZE = 1e5 + 5;

void solve() {
    bool all_negative = true;
    bool all_less_than_n = true;
    int n;
    cin >> n;
    vector<int> a(n + 1);

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (a[i] > 0) {
            all_negative = false;
        }
        if (a[i] < n) {
            all_less_than_n = false;
        }
    }

    if (all_negative) {
        cout << 0 << endl;
    } else if (all_less_than_n) {
        int sum = 0;
        for (int i = 2; i <= n; i++) {
            sum += a[i];
        }
        cout << sum << endl;
    }
}

signed main() {
    int T;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

评测记录

60pts60pts

结合可得。

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 1e5 + 5;
int T;

inline int readInt() {
    int x = 0;
    char c = getchar_unlocked();
    while (c < '0' || c > '9') c = getchar_unlocked();
    while (c >= '0' && c <= '9') {
        x = (x << 3) + (x << 1) + (c - '0');
        c = getchar_unlocked();
    }
    return x;
}

void solve() {
    int n;
    n = readInt();
    vector<int> a(n + 1), dp(n + 1), sum(n + 1);
    int mx = 1, ans = 0;
    bool fg = true;
    
    fill(dp.begin() + 1, dp.end(), 0);
    
    for (int i = 1; i <= n; ++i) {
        a[i] = readInt();
        sum[i] = sum[i - 1] + a[i];
        if (a[i] < n) fg = false;
    }
    
    if (fg) {
        for (int i = 2; i <= n; ++i) {
            ans += a[i];
        }
        cout << ans << "\n";
        return;
    }
    
    for (int i = 1; i <= n; ++i) {
        if (i > mx) break;
        for (int j = i + 1; j <= min(i + a[i], n); ++j) {
            dp[j] = max(dp[j], dp[i] + sum[j - 1] - sum[i]);
        }
        mx = max(mx, i + a[i]);
        ans = max(ans, dp[i] + a[i]);
    }
    
    cout << ans << "\n";
}

signed main() {
    T = readInt();
    while (T--) solve();
    return 0;
}

提交记录

正解

之前扯淡。赛时想到了线段树维护,但是没写出来(滑稽)。设定一个动态转移方程,意思是为拿不拿 aia_i 以及开完之后拿多少个。明显 O(n2)O(n^2),在之前的乱搞途中我们也可以发现,后面的点就算开快读也啥用没有,考虑优化。本人想到线段树的原因是线段树的复杂度是 O(nlog⁡n)O(n \log n) 的,很符合查理,而又没有其它的方案(我不会),所以选择的。

但是我们如果理性分析的话,我们可以把这个式子拆开,就等价于从 i+1i+1 至 i+aii+a_i 求解区间最大值,i+1i+1 到 nn 每个加上 aia_i 和 ii 单独加 dpidp_i。这些分别是区间最大值、区间修改、单点修改,这些线段树都可以高效率解决,故此使用线段树。

线段树代码先不放了,因为官方已经给了。

原文