您的位置:首页 > 其它

hdu3401 Trade 单调队列优化DP

2015-05-21 22:21 381 查看
[align=left]Problem Description[/align]
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?

[align=left]Input[/align]
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.

[align=left]Output[/align]
The most money lxhgww can earn.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

3


有N天,每天你最多能拥有的股票数是MaxP,两次操作间隔必须大于W天(买和卖都算操作)。接下来N行给出每天买一股的价格APi,卖一股的价格BPi,每天最多买ASi股,最多卖BSi股,问最大收入是多少,一开始资金无限。

用dp[i][j]表示第i天拥有股票j的最大收入,以第j天买股票为例,那么dp[i][j]=max(dp[i][j],dp[i-W-1][k]+(j-k)*ap[i]) (j-k<=as[i]),对于每个j遍历k会超时。不难发现只要在j之前找一个k使dp[i-W-1][k]-k*ap[i](设为value)最大并且j-k>=as[i],那么只需要在j从0增大的过程中维护一个value递减序列,对于每个j从当前队列front开始找到第一个满足购买数量限制的,从而得到dp[i][j]。

卖和买原理是一样的。注意初始化。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;

const int MAXN=2010;
const int MAXM=20010;
const LL MOD=1e9+7;
const int INF=0x3f3f3f3f;

int T;
int N,P,W;
int ap[MAXN],bp[MAXN],as[MAXN],bs[MAXN];
int dp[MAXN][MAXN];

struct Stock{
int p,value;
}q[MAXN];

int main(){
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&N,&P,&W);
for(int i=1;i<=N;i++) scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
for(int i=0;i<=N;i++)
for(int j=0;j<=P;j++) dp[i][j]=-INF;
dp[0][0]=0;
for(int i=1;i<=N;i++){
for(int j=0;j<=P;j++) dp[i][j]=max(dp[i][j],dp[i-1][j]);
if(i-W-1<=0){
for(int j=0;j<=as[i];j++) dp[i][j]=max(dp[i][j],-ap[i]*j);
}
else{
//买
int front=0,rear=-1;
for(int j=0;j<=P;j++){
Stock tmp=(Stock){j,dp[i-W-1][j]+j*ap[i]};
while(front<=rear&&q[rear].value<=tmp.value) rear--;
q[++rear]=tmp;
while(front<=rear&&q[front].p+as[i]<j) front++;
dp[i][j]=max(dp[i][j],q[front].value-ap[i]*j);
}
//卖
front=0;
rear=-1;
for(int j=P;j>=0;j--){
Stock tmp=(Stock){j,dp[i-W-1][j]+j*bp[i]};
while(front<=rear&&q[rear].value<=tmp.value) rear--;
q[++rear]=tmp;
while(front<=rear&&q[front].p-bs[i]>j) front++;
dp[i][j]=max(dp[i][j],q[front].value-bp[i]*j);
}
}
}
int ans=0;
for(int i=0;i<=P;i++) ans=max(ans,dp
[i]);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: