AcWing 1056. 股票买卖 III
原题链接
简单
作者:
wjie
,
2020-07-10 12:45:31
,
所有人可见
,
阅读 510
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int arr[N], f[N][3][2];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
memset(f, -0x3f, sizeof(f));
f[1][0][0] = 0;
f[1][1][1] = -arr[1];
for (int i = 2; i <= n; ++i)
{
f[i][0][0] = 0;
for (int j = 1; j <= 2; ++j)
{
f[i][j][0] = max(f[i-1][j][0], f[i-1][j][1]+arr[i]);
f[i][j][1] = max(f[i-1][j][1], f[i-1][j-1][0]-arr[i]);
}
}
int maxv = max(f[n][0][0], f[n][1][0]);
printf("%d", max(maxv, f[n][2][0]));
return 0;
}