AcWing 1479. 最大子序列和
原题链接
中等
作者:
Wegoon
,
2021-11-09 18:44:30
,
所有人可见
,
阅读 256
知识点:贪心、DP
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;
int n, a[N];
struct node {
int maxn, l, r;
} dp[N], res;
int main() {
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
dp[0].maxn = res.maxn = -1;
for (int i = 1; i <= n; i ++ ) {
if(dp[i - 1].maxn >= 0 ) {
dp[i].maxn = dp[i - 1].maxn + a[i];
dp[i].l = dp[i - 1].l;
dp[i].r = i;
}else {
dp[i].maxn = a[i];
dp[i].l = dp[i].r = i;
}
if(res.maxn < dp[i].maxn) res = dp[i];
}
if(!res.l && !res.r) res.maxn = 0, res.l = 1, res.r = n;
cout << res.maxn << ' ' << a[res.l] << ' ' << a[res.r] << endl;
return 0;
}