股票买卖Ⅳ

给定一个长度为 N 的数组,数组中的第 i 个数字表示一个给定股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润,你最多可以完成 k 笔交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。一次买入卖出合为一笔交易。

输入格式

第一行包含整数 N 和 k,表示数组的长度以及你可以完成的最大交易数量。

第二行包含 N 个不超过 10000 的正整数,表示完整的数组。

输出格式

输出一个整数,表示最大利润。

数据范围

1≤N≤1e5
1≤k≤100

输入样例1:

3 2
2 4 1

输出样例1:

2

输入样例2:

6 2
3 2 6 5 0 3

输出样例2:

7

样例解释

样例1:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。

样例2:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。共计利润 4+3 = 7.

dp[i][j][0] 表示第i天进行第j次卖出,那么第j次买入在i之前,所以dp[i][j][0]可以由dp[i - 1][j][1] + w[i]转来

dp[i][j][1] 表示第i天进行第j次的买入,那么第j-1次交易必须完成,所以dp[i][j][1]可以由dp[i - 1][j - 1][0] - w[i]转来

#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128;template <class T>istream& read(T& x, istream& cin = std::cin);template <class T>ostream& write(T x, ostream& cout = std::cout);ostream& operator<<(ostream& cout, lll x);istream& operator>>(istream& cin, lll &x);
bool check(int i, int j);

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

int n, m, dp[N][M][2], w[N];

void init() {
	memset(dp, -0x3f, sizeof dp);
	cin >> n >> m;
	dp[0][0][0] = 0;
	for (int i = 1; i <= n; i++) {
		dp[i][0][0] = 0;
		cin >> w[i];
	}
	//	cout << "初始化完成\n";
	return;
}

void solve() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1] + w[i]);  //第 i 天进行第 j 笔交易的卖出与第 i 天之前就卖出哪个最优
			dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - w[i]);  // 第 i 天进行第 j 笔交易的买入与第 i 天之前买入哪个最优
		}
	}
	int res = 0;
	for (int i = 0; i <= m; i++)
		res = max(dp[n][i][0], res);
	cout << res;
	return;
}

int main(void) {
	ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;//保留6位小数从小数点后计数
	int TT = 1;
	//cin >> TT;
	for (int ii = 0; ii < TT; init(), solve(), ii++, cout << "\n") {}
	return 0;
}
//template <class T> istream& read(T& x, istream& cin) {T num = 0; bool f = 0; char ch = 0;while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) return cin; }x = f ? -num : num; return cin;}template <class T> ostream& write(T x, ostream& cout) {if (x < 0) cout.put('-'), x = -x;if (x > 9) write(x / 10);cout.put(x % 10 + '0'); return cout;}ostream& operator<<(ostream& cout, lll x) {write(x);return cout;}istream& operator>>(istream& cin, lll &x) {return read(x);}

两种写法都可以

#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128;template <class T>istream& read(T& x, istream& cin = std::cin);template <class T>ostream& write(T x, ostream& cout = std::cout);ostream& operator<<(ostream& cout, lll x);istream& operator>>(istream& cin, lll &x);
bool check(int i, int j);

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

int n, m, dp[N][M][2], w[N];

void init() {
	memset(dp, -0x3f, sizeof dp);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		dp[i][0][0] = 0;
		cin >> w[i];
	}
	//	cout << "初始化完成\n";
	return;
}

void dfs(int u) {
    if (!u) {
        dp[u][0][0] = 0;
        return;
    }
    dfs(u - 1);
    for (int j = 1; j <= m; j ++) {
        dp[u][j][0] = max(dp[u - 1][j][0], dp[u - 1][j][1] + w[u]);  //第 i 天进行第 j 笔交易的卖出与第 i 天之前就卖出哪个最优
		dp[u][j][1] = max(dp[u - 1][j][1], dp[u - 1][j - 1][0] - w[u]);  // 第 i 天进行第 j 笔交易的买入与第 i 天之前买入哪个最优
    }
}

void solve() {
    dfs(n);
	int res = 0;
	for (int i = 0; i <= m; i++)
		res = max(dp[n][i][0], res);
	cout << res;
	return;
}

int main(void) {
	ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;//保留6位小数从小数点后计数
	int TT = 1;
	//cin >> TT;
	for (int ii = 0; ii < TT; init(), solve(), ii++, cout << "\n") {}
	return 0;
}
//template <class T> istream& read(T& x, istream& cin) {T num = 0; bool f = 0; char ch = 0;while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) return cin; }x = f ? -num : num; return cin;}template <class T> ostream& write(T x, ostream& cout) {if (x < 0) cout.put('-'), x = -x;if (x > 9) write(x / 10);cout.put(x % 10 + '0'); return cout;}ostream& operator<<(ostream& cout, lll x) {write(x);return cout;}istream& operator>>(istream& cin, lll &x) {return read(x);}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值