Stock Trading IV

Stock Trading IV

Title:

Given an array of length N, the i-th number in the array represents the price of a given stock on the i-th day.

Design an algorithm to calculate the maximum profit you can get. You can complete k transactions at most.

Note: You cannot participate in multiple transactions at the same time (you must sell the previous stocks before buying again). One purchase and sale are combined into one transaction.

answer:

Let us suppose that dp[i][j][0] represents the selection from the previous i day, and a total of j transactions are performed. The current state is the maximum return of not holding stocks
dp[i][j][1] represents the selection from the previous i day, There are j transactions in total, and the current status is the maximum return of holding stocks. The
third dimension shows the current status.
For dp[i][j][0], there are no stocks at present, then we can inherit from the previous day’s absence of stocks. Come, it can also be inherited from the stock sold the day before.
For dp[i][j][0], it means that there is currently stock, it may be inherited from the stock the day before, or it may be that there is no stock the day before today. Just bought the
biggest situation in the above situation. The
last day, the largest number of transactions, and the final holding of no stock is the result. To
compress the space, we use a rolling array to optimize the first dimension (in fact, the compression of the 01 knapsack problem)

Code:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 1e5 + 10, M = 110;

int n, m;
int w[N];
int f[N][M][2];     //f[i][j][0]代表从前i天中选,共进行了j次交易,当前状态为不持有股票的最大收益

int main()
{
    
    
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);

    memset(f, -0x3f, sizeof f);                     //要求最大值,先初始化为负无穷
    for (int i = 0; i <= n; i++) f[i][0][0] = 0;    //不管几天,只要没有交易收益就是0  

    for (int i = 1; i <= n; i++) {
    
                      //状态机见上图
        for (int j = 1; j <= m; j++) {
    
    
            f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1] + w[i]);
            f[i][j][1] = max(f[i - 1][j][1], f[i - 1][j - 1][0] - w[i]);
        }
    }

    int res = 0;
    //最后一天,共交易k次,且最后不持有股票的最大值即为结果
    for (int k = 0; k <= m; k ++ ) res = max(res, f[n][k][0]);  

    printf("%d\n", res);

    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
typedef long long ll;
#define MAXK 111
ll f[2][MAXK];
int main()
{
    
    
    ll n,k,x;
    scanf("%lld%lld",&n,&k);
    memset(f,0xcf,sizeof f);
    f[0][0]=0;
    for(ll i=1;i<=n;++i)
    {
    
    
        scanf("%lld",&x);
        for(ll j=k;j;--j)
        {
    
    
            f[0][j]=std::max(f[0][j],f[1][j]+x);
            f[1][j]=std::max(f[1][j],f[0][j-1]-x);
        }
    }
    ll ans=0;
    for(ll i=0;i<=k;++i)ans=std::max(ans,std::max(f[0][i],f[1][i]));
    printf("%lld",ans);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_35975367/article/details/114601069