HDU - 3401 Trade(单调队列优化dp)

Recently, lxhgww is addicted to stock, he finds some regular patterns after a few days' study. 
He forecasts the next T days' stock market. On the i'th day, you can buy one stock with the price APi or sell one stock to get BPi. 
There are some other limits, one can buy at most ASi stocks on the i'th day and at most sell BSi stocks. 
Two trading days should have a interval of more than W days. That is to say, suppose you traded (any buy or sell stocks is regarded as a trade)on the i'th day, the next trading day must be on the (i+W+1)th day or later. 
What's more, one can own no more than MaxP stocks at any time. 

Before the first day, lxhgww already has infinitely money but no stocks, of course he wants to earn as much money as possible from the stock market. So the question comes, how much at most can he earn? 

Input

The first line is an integer t, the case number. 
The first line of each case are three integers T , MaxP , W . 
(0 <= W < T <= 2000, 1 <= MaxP <= 2000) . 
The next T lines each has four integers APi,BPi,ASi,BSi( 1<=BPi<=APi<=1000,1<=ASi,BSi<=MaxP), which are mentioned above. 

Output

The most money lxhgww can earn.

Sample Input

1
5 2 0
2 1 1 1
2 1 1 1
3 2 1 1
4 3 1 1
5 4 1 1

Sample Output

3

            可以得到dp式 :(1)dp[i][j] = max(dp[i][j],dp[i-1][j]); (2) dp[i][j] = max(dp[i][j],dp[r][k]-ap[i]*(j-k))   买进  

                                      (3)dp[i][j] = max(dp[i][j],dp[r][k]+bp[i]*(k-j)) 卖出

           正常dp式需要n^3的复杂度,这种情况下我们将 买入式子化成    dp[i][j]=(dp[r][k]+ap[i] * k)- ap[i] * j;这样的话利用单调栈可以来在o(n)内获得符合数量限制条件的最大值(dp[r][k]+ap[i] * k)了 。卖出也一样

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
int ap[maxn], bp[maxn];
int as[maxn], bs[maxn];
int dp[maxn][maxn];
struct node { int idx, kk; };
node q[2005];
int n, m, w, ans;
int main() {
	int te;
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> te;
	while (te--) {
		cin >> n >> m >> w;
		for (int i = 1; i <= n; i++) {
			cin >> ap[i] >> bp[i] >> as[i] >> bs[i];
		}
		for (int i = 0; i <= n; i++)
			for (int j = 0; j <= m; j++)
				dp[i][j] = -0x3f3f3f3f;
		for (int i = 1; i <= w + 1; i++)
			for (int j = 0; j <= min(as[i], m); j++)
				dp[i][j] = -j * ap[i];
		dp[0][0] = 0;
		int head = 0, tail = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= m; j++) {
				dp[i][j] = max(dp[i][j], dp[i - 1][j]);
			}
			if (i <= w + 1)continue;
			int g = i - w - 1, gg;
			head = 0; tail = 0;
			for (int j = 0; j <= m; j++) {
				gg = dp[g][j] + j * ap[i];
				while (head < tail&&q[tail - 1].kk < gg)
					tail--;
				q[tail].idx = j; q[tail++].kk = gg;
				while (head < tail&&q[head].idx + as[i] < j) 
					head++;
				dp[i][j] = max(dp[i][j], q[head].kk - j * ap[i]);
			}
			head = 0; tail = 0;
			for (int j = m; j >= 0; j--) 
			{
				gg = dp[g][j] + j * bp[i];
				while (head < tail&&q[tail - 1].kk < gg)
					tail--;
				q[tail].idx = j; q[tail++].kk = gg;
				while (head<tail&&q[head].idx - bs[i]>j)
					head++;
				dp[i][j] = max(dp[i][j], q[head].kk - j * bp[i]);
			}
		}
		ans = 0;
		for (int i = 0; i <= m; i++)
			ans = max(ans, dp[n][i]);
		printf("%d\n", ans);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值