Files
python/TangDou/AcWing/1055_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

27 lines
957 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int n;
int w[N];
int f[N][2];//考虑前i天的股市第i天的手中股票状态为j(j=0 未持股j=1 持股)
int main() {
cin >> n;
for (int i = 1; i <= n; i++)cin >> w[i];
/**
因为f[0][1]代表考虑前0天状态是已经买入这是一个不合法状态。(一般不合法的状态设置为INF或者-INF要看取最大值或最小值)
第一次买入至少是在第1天。
如果不把它初始化为负无穷f[1][0]就会更新成f[0][1] + w[1]也就是w[1]
但是f[1][0]实际意义是第 1 天不买入股票,那么收益就是 0而不是w[i]
*/
f[0][1] = -INF;
for (int i = 1; i <= n; i++) {
f[i][0] = max(f[i - 1][0], f[i - 1][1] + w[i]);
f[i][1] = max(f[i - 1][1], f[i - 1][0] - w[i]);
}
printf("%d\n", f[n][0]);
return 0;
}