Trade【单调队列】

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6532 Accepted Submission(s): 2283


Problem Description
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


中文翻译
最近, l x h g w w lxhgww lxhgww沉迷于股票,经过几天的学习,他发现了一些常规模式。他可以预测未来的T天股市。 在第一天,你可以买价格 A P i APi APi或卖价格 B P i BPi BPi的股票。
还有一些其他限制,在一天最多可以买到 A S i ASi ASi股票,最多可以卖 B S i BSi BSi股票。
两个交易日的间隔应超过W天。 也就是说,假设您在第二天交易(任何买入或卖出股票被视为交易),则下一个交易日必须在第( i + W + 1 i + W + 1 i+W+1)天或之后。
而且,任何时候都不能拥有MaxP股票。
在第一天之前, l x h g w w lxhgww lxhgww已经有无限的钱,但没有股票,当然他想从股票市场赚取尽可能多的钱。 所以问题来了,他能赚多少钱?
PS:(我终于从看完题不会做到连题都看不懂了)


解题思路
d p dp dp+单调队列优化

定义 d p dp dp数组 d p [ i ] [ j ] dp[i][j] dp[i][j],表示第i天买j只股票所能获得的最大收益。
则买入的状态转移方程:
d p [ i ] [ j ] = m a x ( d p [ i ] [ j ] , d p [ i − W − 1 ] [ k ] − ( j − k ) ∗ A p [ i ] ) = m a x ( d p [ i ] [ j ] , d p [ i − W − 1 ] [ k ] + k ∗ A p [ i ] − j ∗ A p [ i ] ) dp[i][j]=max(dp[i][j],dp[i-W-1][k]-(j-k)*Ap[i])=max(dp[i][j],dp[i-W-1][k]+k*Ap[i]-j*Ap[i]) dp[i][j]=max(dp[i][j],dp[iW1][k](jk)Ap[i])=max(dp[i][j],dp[iW1][k]+kAp[i]jAp[i])
卖出的状态转移方程:
d p [ i ] [ j ] = m a x ( d p [ i ] [ j ] , d p [ i − W − 1 ] [ k ] + ( k − j ) ∗ B p [ i ] ) = m a x ( d p [ i ] [ j ] , d p [ i − W − 1 ] [ k ] + k ∗ B p [ i ] − j ∗ B p [ i ] ) dp[i][j]=max(dp[i][j],dp[i-W-1][k]+(k-j)*Bp[i])=max(dp[i][j],dp[i-W-1][k]+k*Bp[i]-j*Bp[i]) dp[i][j]=max(dp[i][j],dp[iW1][k]+(kj)Bp[i])=max(dp[i][j],dp[iW1][k]+kBp[i]jBp[i])
无论是买入或卖出, d p [ i − W − 1 ] [ k ] + k ∗ X p [ i ] dp[i-W-1][k]+k*Xp[i] dp[iW1][k]+kXp[i]是跟 k k k相关的值, j ∗ X p [ i ] j*Xp[i] jXp[i]是与j相关的值
而且题目要求是最大收益,对于同一个j,枚举所有的 k k k d p [ i − W − 1 ] [ k ] + k ∗ X p [ i ] dp[i-W-1][k]+k*Xp[i] dp[iW1][k]+kXp[i]的最大
值就好了,那么就可以将这个式子通过单调队列来维护。
注意最后求值的时候,还要保证 a b s ( k − j ) < = X s [ i ] abs(k-j)<=Xs[i] abs(kj)<=Xs[i],即买入或卖出的数量不超过限制。


代码

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int T,tt,maxp,w,ans,h,t,f[3010][3010]; 
int as[3010],bs[3010],ap[3010],bp[3010];
struct c{
   int x,y;
}q[3010*2];
void buy(int i,int j){
   while(h<t&&q[t-1].x<f[i-w-1][j]+j*ap[i])t--;
   q[t].x=f[i-w-1][j]+j*ap[i];
   q[t++].y=j;
   while((h==-1)||(h<t&&j-q[h].y>as[i]))
   	h++;
   f[i][j]=max(f[i][j],q[h].x-j*ap[i]);
}
void sell(int i,int j){
   while(h<t&&q[t-1].x<f[i-w-1][j]+j*bp[i])t--;
   q[t].x=f[i-w-1][j]+j*bp[i];
   q[t++].y=j;
   while(h==-1||(h<t&&q[h].y-j>bs[i]))
   	h++;
   f[i][j]=max(f[i][j],q[h].x-j*bp[i]);
}
int main(){
   scanf("%d",&T);
   while(T--)
   {
   	scanf("%d%d%d",&tt,&maxp,&w);
   	for(int i=1;i<=tt;i++)
   		scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
   	for(int i=0;i<=tt;i++)
   		for(int j=0;j<=maxp;j++) 
   			f[i][j]=-1e8;
   	for(int i=1;i<=tt;i++)//任意一天不买不卖所获得的收益为0
   		f[i][0]=0;
   	for(int i=1;i<=w+1;i++)
   		for(int j=0;j<=as[i];j++) 
   			f[i][j]=-j*ap[i];//前w+1天,只能买进
   	f[0][0]=0;
   	for(int i=2;i<=tt;i++)
   	{                                              
   		for(int j=0;j<=maxp;j++)     
   			f[i][j]=max(f[i][j],f[i-1][j]);//不买不卖时的最大值
   		if(i<=w+1) continue; 
   		h=t=0;
   		for(int j=0;j<=maxp;j++)//因为是买进,所以比应该正序,保证比当前买的数目小的状态都已算过
   			buy(i,j);
   		h=t=0;
   		for(int j=maxp;j>=0;j--)//相反...
   			sell(i,j);
   	}	
   	ans=-1e8;
   	for(int i=0;i<=maxp;i++)
   		ans=max(ans,f[tt][i]);	
   	printf("%d\n",ans);
   }
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值