传送门
题目描述
给你一个长度为 n n n的序列 a i a_{i} ai,还有一个整数 x x x,你可以给任意一个子区间的每一个数都乘上 x x x,求处理过后的序列的最大子序列和是多少
分析
分三个状态来求解
1.未处理,2.正在处理,3.已经结束处理
1状态只能由1状态进行转移,2状态可以由1状态转移也可以由2状态转移,3状态可以由2状态转移也可以由3状态转移,然后线性DP搞搞就出来了
需要注意的是答案最小值必然为0
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 3e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
ll f[N][3]; //0 未使用 1 正在使用 2 使用结束
int n, a[N];
ll minv[N][3], x;
int main() {
read(n), read(x);
for (int i = 1; i <= n; i++) read(a[i]);
ll ans = -0x3f3f3f3f3f3f3f3f;
for (int i = 1; i <= n; i++) {
f[i][0] = max(f[i - 1][0] + a[i], 1ll * 0);
f[i][1] = max(f[i - 1][0] + a[i] * x, max(f[i - 1][1] + a[i] * x, 1ll * 0));
f[i][2] = max(f[i - 1][2] + a[i], max(f[i - 1][1] + a[i], 1ll * 0));
ans = max(ans, max(max(f[i][0], f[i][1]), f[i][2]));
}
dl(ans);
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/