其他分享
首页 > 其他分享> > AcWing1057. 股票买卖 IV

AcWing1057. 股票买卖 IV

作者:互联网

题目

1057. 股票买卖 IV

思路

代码

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100005;
const int maxm = 105;
const int INF = 0x3f3f3f3f;
int w[maxn],f[maxn][maxm][2];
int main() {
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif // ONLINE_JUDGE
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; ++i) cin >> w[i];
    for(int i = 0; i <= m; ++i) {
        f[0][i][0] = f[0][i][1] = -INF;
    }
    for(int i = 0; i <= n; ++i) {
        f[i][0][0] = 0;
        f[i][0][1] = -INF;
    }
    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;
    for(int i = 0; i <= m; ++i) res = max(res, f[n][i][0]);
    cout << res;
    return 0;
}

标签:股票买卖,没货,状态,int,天时,AcWing1057,IV,交易,赋值
来源: https://www.cnblogs.com/lemonsbiscuit/p/16341067.html