Title Description: Stock Trading IV
Given an array of length N, the number i in the array represents the price of a given stock on day i. Design an algorithm to calculate the maximum profit you can make, and you can complete up to k transactions.
Note: You cannot participate in multiple transactions at the same time (you must sell your previous shares before you buy them again). One purchase and one sale make one transaction.
Input Format Design
The first line contains the integers N and k, indicating the length of the array and the maximum number of transactions you can complete.
The second line contains N positive integers not exceeding 1000, representing the complete array.
Output Format
Output an integer representing the maximum profit.
Data Range
1≤N≤10^5,
1≤k≤100
Input Sample 1:
3 2
2 4 1
Output Sample 1:
2
Input Sample 2:
6 2
3 2 6 5 0 3
Output Sample 2:
7
Sample Interpretation
Example 1: Buy on day 1 (stock price = 2) and sell on day 2 (stock price = 4), and the exchange will make a profit = 4-2 = 2.
Example 2: Buy on the second day (stock price = 2) and sell on the third day (stock price = 6), and the exchange will make a profit = 6-2 = 4. Subsequently, buy on the fifth day (stock price = 0) and sell on the sixth day (stock price = 3), and the exchange will make a profit = 3-0 = 3. The total profit is 4+3 = 7.
Analysis:
This topic requires that transactions not exceed the maximum profit earned by k transactions, one transaction being a transaction.
According to the state machine in the figure above, i f the stock is held in the current state, you can choose to continue to hold it or sell it the next day; i f the current state is not held, you can continue to buy without holding it the next day. Because of the limit of k transactions, one dimension must be added to the state representation to indicate the number of transactions that have already been made, where a purchase is considered to have started a transaction.[j][0] indicates that J transactions have been made by day I and no positions are held at this time, and f[i][j][1] indicates that J transactions have been made by day I and that there are positions at this time. First, consider that the status of the day before reaching the status of f[i][j][0] can be either 0 or 1. If the status of the day before is 0, it means that the day before was not held and a jtransaction has been made, that is, f[i][j][0] = f[i-1][j][0]I f there is a position on the previous day, it means that the stock was sold on the day i-1, and the status on the day I-1 is f[i-1][j][1], f[i][j][0] = f[i-1][j][1] + w[i], plus W is sold to account w yuan, so f[i][j][0] = max(f[i-1][j][0],f[i-1][j][1]+w[i]). Similarly, to reach the status f[i][j][1][1]+w[i].If the state of the previous day is 0, the stocks are purchased on the iday, and the jth transaction is made on the iday, and only J-1 transaction is made on the preceding day, that is, f[i][j][1] = f[i-1][j-1][j-1][0] - w[i], subtract w means that the wyuan is deducted for the stocks purchased, w [i][j][1] = f [j][1] = f[i[j] [1] = f[i [1] = f[i] [j] [1] = max [f [i] [i] [i] [i] [i] [1] [j] [1] [j] [1] [j] [1], f [i]-1][j-1][0]-w[i])The state transition equation is then derived. Consider the boundary state below. f[i][0][0] indicates that no transaction was made on day I I and the return is 0. In addition, the initial state of f[i][[j][0] and f[i][0][1] should be illegal and set to -INF.
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 100005,M = 105; int f[N][M][2]; int main(){ int n,k,price; scanf("%d%d",&n,&k); memset(f,-0x3f,sizeof f); f[0][0][0] = 0; for(int i = 1;i <= n;i++){ scanf("%d",&price); f[i][0][0] = 0; for(int j = 1;j <= k;j++){ f[i][j][0] = max(f[i-1][j][0],f[i-1][j][1]+price); f[i][j][1] = max(f[i-1][j-1][0]-price,f[i-1][j][1]); } } int res = 0; for(int i = 0;i <= k;i++) res = max(res,f[n][i][0]); printf("%d\n",res); return 0; }
Since the state on day I only uses the state on day i-1, it can be implemented with a rolling array, requiring an inverted enumeration of transactions to prevent the required state from being overwritten.
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int M = 105; int f[M][2]; int main(){ int n,k,price; scanf("%d%d",&n,&k); memset(f,-0x3f,sizeof f); f[0][0] = 0; for(int i = 1;i <= n;i++){ scanf("%d",&price); for(int j = k;j >= 1;j--){ f[j][0] = max(f[j][0],f[j][1]+price); f[j][1] = max(f[j-1][0]-price,f[j][1]); } } int res = 0; for(int i = 0;i <= k;i++) res = max(res,f[i][0]); printf("%d\n",res); return 0; } For reference only.