Codeforces - 1155D - Beautiful Array
Codeforces - 1155D - Beautiful Array
地址
https://codeforces.com/contest/1155/problem/D
原文地址
https://www.lucien.ink/archives/420/
题意
你有一个长为 $n$ 的序列,你可以选择一个子区间(可以为空),将这个子区间所有的元素乘以 $x$ ,问做完上述操作之后这个序列的最大连续区间和可以是多少。
题解
记状态为 f[i][status]
,其中 $status \in {used, unused, inuse}$
used
代表已经使用过了 $x$ ,unused
代表暂时还没使用 $x$ ,inuse
代表正在使用 $x$ ,转移一下即可。
代码
https://pasteme.cn/6690
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
## include <bits/stdc++.h>
const int maxn = int(3e5) + 7;
typedef long long ll;
enum { used, inuse, unused };
ll n, x, ans, a[maxn], f[maxn][3];
int main() {
std::cin >> n >> x;
for (int i = 1; i <= n; i++) std::cin >> a[i];
for (int i = 1; i <= n; i++) {
f[i][unused] = std::max(f[i - 1][unused] + a[i], a[i]);
f[i][inuse] = std::max({f[i - 1][unused] + a[i] * x, f[i - 1][inuse] + a[i] * x, a[i] * x});
f[i][used] = std::max({f[i - 1][inuse] + a[i], f[i - 1][used] + a[i], a[i]});
for (int j = 0; j < 3; j++) ans = std::max(ans, f[i][j]);
}
std::cout << ans << std::endl;
return 0;
}
This post is licensed under CC BY 4.0 by the author.