Educational Codeforces Round 88 (Rated for Div. 2) D. Yet Another Yet Another Task(枚举/最大连续子序列)

题目链接:https://codeforces.com/contest/1359/problem/D

题意

有一个大小为 $n$ 的数组,可以选取一段连续区间去掉其中的最大值求和,问求和的最大值为多少。

题解

枚举所有可能的情况,其中一定有一个是正确答案。

即每次枚举去掉的最大值,取最大连续子序列的和。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; cin >> n;
    int a[n] = {};
    for (int i = 0; i < n; i++) 
        cin >> a[i];
    int ans = 0;
    for (int maxn = 30; maxn >= -30; maxn--) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] <= maxn) {
                sum = max(0, sum) + a[i];
                ans = max(ans, sum - maxn);
            }
        }
    }
    cout << ans << "
";
}
原文地址:https://www.cnblogs.com/Kanoon/p/12989145.html