C++ --- state machine model --- stock trading V (one algorithm per day 2023.4.13)

Note:
This topic is an extended topic of "State Machine Model-Stock Trading IV" . It is recommended to read this article first and understand it.

Question:
Given an array of length N, the i-th number in the array represents the price of a given stock on day i.
Design an algorithm to calculate the maximum profit. You can complete as many transactions as possible (buy and sell stocks multiple times) subject to the following constraints:

  • You cannot participate in multiple transactions at the same time (you must sell the previous stock before buying it again).
  • After selling the stock, you cannot buy the stock the next day (that is, the freezing period is 1 day).

Input Format
The first line contains the integer N, representing the length of the array.
The second line contains N positive integers not exceeding 10000, representing a complete array.

Output format
Output an integer, representing the maximum profit.

Data range
1≤N≤10^5

The example explains
that the corresponding transaction status is: [buy, sell, freeze period, buy, sell], the first transaction can get a profit of 2-1 = 1, and the second transaction can get a profit of 2-0 = 2. Total profit 1+2 = 3.

输入:
5
1 2 3 0 2
输出:
3
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 100010;
int n;
int w[N], f[N][3];      //w[i]为第i天的 卖出/买入 的价值

int main() {
    
    
    cin >> n;
    for (int i = 1; i<=n; i++) cin >> w[i];
    
    //设置状态机的入口
    //从实际意义出发,第0天不可能持有股票同时也不可能是冷冻期,所以设置为负无穷(因为要求的是最大值Max),
    //第0天未持有股票且能自由购入是合法的,此时价值为0,
    f[0][0] = f[0][1] = -0x3f3f3f;
    f[0][2] = 0;
    
    //按状态机分析进行状态转移
    for (int i = 1; i<=n; i++) {
    
    
        f[i][0] = max(f[i-1][0], f[i-1][2]-w[i]);
        f[i][1] = f[i-1][0] + w[i];
        f[i][2] = max(f[i-1][1], f[i-1][2]);
    }
    
    //状态机的两个出口取max
    cout << max(f[n][1], f[n][2]);
    return 0;
}

Idea:
classic y-style dp analysis method

1. Status representation
f[i][0] : For all plans that currently hold stocks in the previous i day,
f[i][1]: For all plans that are in the first day after selling (frozen period) in the previous i day, :
f[i][2]For all plans that are currently sold in the previous i day For all plans on the nth day (n>=2) (free buy) after release,
the attribute is Max.

2. State calculation
It is enough to transfer directly according to the state machine analysis,
f[i][0] = max(f[i-1][0], f[i-1][2]-w[i]),
f[i][1] = f[i-1][0] + w[i],
f[i][2] = max(f[i-1][1], f[i-1][2]),

State machine diagram:

Please add a picture description

If it is helpful, please give a free like~ Someone watching is the motivation to support me to write down!

Disclaimer:
The source of the algorithm idea is Mr. Y. For details, please refer to https://www.acwing.com/
This article is only used for learning records and exchanges

Guess you like

Origin blog.csdn.net/SRestia/article/details/130131979