传送门
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
Author
lxhgww
Source
HDOJ Monthly Contest – 2010.05.01
Recommend
lcy | We have carefully selected several similar problems for you: 3400 3403 3404 3402 3415
题目大意
知道未来 T T T天的股票买入价格和卖出价格,在满足以下条件下求所能获得的最大收益。
- 每天的股票买价 A P i APi APi,卖价 B P i BPi BPi,买和卖都有限制,只能买 A S i ASi ASi支,卖 B S i BSi BSi支
- 每做一次操作(买或卖)都要隔 W W W天才能做下一次操作
- 手上的股票不可以超过 M a x P MaxP MaxP支
解题思路
在此推荐某不知名巨佬的博客
DP
设
f
[
i
]
[
j
]
f[i][j]
f[i][j]表示到第
i
i
i天拥有
j
j
j支股票
最终答案
m
a
x
(
f
[
i
]
[
j
]
)
max(f[i][j])
max(f[i][j])(0<=i<=n,0<=j<=MaxP)
购买
f[i][j]=max(f[i][j],f[i-w-1][k]-(j-k)*api[i]
=max(f[i][j],f[i-w-1][k]+k*api[i]-j*api[i]
卖出
f[i][j]=max(f[i][j],f[i-w-1][k]+(k-j)*bpi[i]
=max(f[i][j],f[i-w-1][k]+k*bpi[i]-j*bpi[i]
可以看出
f
[
i
−
W
−
1
]
[
k
]
+
k
∗
X
p
[
i
]
f[i-W-1][k]+k*Xp[i]
f[i−W−1][k]+k∗Xp[i]是跟k相关的值,
j
∗
X
p
[
i
]
j*Xp[i]
j∗Xp[i]是与j相关的值
j
j
j可以直接枚举,而关于
k
k
k的式子则可以用单调队列优化
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int INF=0x7fffffff;
struct DT{
int v,num;
}que[2010*2];
int head,tail,T,t,maxp,w,ans;
int api[2010],bpi[2010],asi[2010],bsi[2010],f[2010][2010];
void buy(int i,int j){//买入的状态转移
while(head<tail&&que[tail-1].v<f[i-w-1][j]+j*api[i])tail--;
que[tail].v=f[i-w-1][j]+j*api[i],que[tail++].num=j;
while((head==-1)||(head<tail&&j-que[head].num>asi[i]))head++;//某不知名巨佬说会有h==-1的情况
if(que[head].v-j*api[i]>f[i][j])
f[i][j]=que[head].v-j*api[i];
}
void sell(int i,int j){//卖出的状态转移
while(head<tail&&que[tail-1].v<f[i-w-1][j]+j*bpi[i])tail--;
que[tail].v=f[i-w-1][j]+j*bpi[i],que[tail++].num=j;
while((head==-1)||(head<tail&&que[head].num-j>bsi[i]))head++;
if(que[head].v-j*bpi[i]>f[i][j])
f[i][j]=que[head].v-j*bpi[i];
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&t,&maxp,&w);
for(int i=1;i<=t;i++)
scanf("%d%d%d%d",&api[i],&bpi[i],&asi[i],&bsi[i]);
memset(f,0xcf,sizeof(f));
for(int i=0;i<=t;i++)f[i][0]=0;//不拥有股票,价值为0
for(int i=1;i<=w+1;i++)//前1~w+1天都可以买,不能卖
for(int j=1;j<=asi[i];j++)
f[i][j]=-j*api[i];
for(int i=2;i<=w+1;i++)//前面有买,但是现在不买不卖
for(int j=0;j<=maxp;j++)
if(f[i-1][j]>f[i][j])
f[i][j]=f[i-1][j];
for(int i=w+2;i<=t;i++){
for(int j=0;j<=maxp;j++)//前面有买卖,但是现在不买不卖
if(f[i-1][j]>f[i][j])
f[i][j]=f[i-1][j];
head=tail=0;
for(int j=0;j<=maxp;j++)//保证价值可能创造多的在前(买的越多价值越小)
buy(i,j);
head=tail=0;
for(int j=maxp;j>=0;j--)//保证价值可能创造多的在前(卖的越多价值越大)
sell(i,j);
}
ans=-INF;
for(int i=0;i<=t;i++)
for(int j=0;j<=maxp;j++)
if(f[i][j]>ans)
ans=f[i][j];
printf("%d\n",ans);
}
}