HDU 3401 Trade 题解
题目
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?
输入
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.
输出
The most money lxhgww can earn.
样例
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
output
3
题意
有T组测试数据
不能拥有超过
m
a
x
p
maxp
maxp张股票
每一次贸易的间隔要超过
w
w
w天(也就是下一次是
w
w
w+1天后)
每天可以买入价为
A
P
I
API
API的不超过
A
S
I
ASI
ASI张股票
每天可以卖出价为
B
P
I
BPI
BPI的不超过
B
S
I
BSI
BSI张股票
求最多能赚多少数
解题思路
每一次的初值就是当天不买不卖
如果在交易限制的w+1天前就跳过本次循环
然后分别进行买入和卖出的操作
买入的方程:
d
p
dp
dp[
i
i
i][
j
j
j]=
m
a
x
max
max(
d
p
dp
dp[
i
i
i][
j
j
j],
d
p
dp
dp[
i
i
i−
W
W
W−1][
k
k
k]−(
j
j
j−
k
k
k)∗
A
p
Ap
Ap[
i
i
i])=
m
a
x
max
max(
d
p
dp
dp[
i
i
i][
j
j
j],
d
p
dp
dp[
i
i
i−
W
W
W−1][
k
k
k]+
k
k
k∗
A
p
Ap
Ap[
i
i
i]−
j
j
j∗
A
p
Ap
Ap[
i
i
i])
卖出的方程:
d
p
dp
dp[
i
i
i][
j
j
j]=
m
a
x
max
max(
d
p
dp
dp[
i
i
i][
j
j
j],
d
p
dp
dp[
i
i
i−
W
W
W−1][
k
k
k]+(
k
k
k−
j
j
j)∗
B
p
Bp
Bp[
i
i
i])=
m
a
x
max
max(
d
p
dp
dp[
i
i
i][
j
j
j],dp[
i
i
i−
W
W
W−1][
k
k
k]+
k
k
k∗
B
p
Bp
Bp[
i
i
i]−
j
j
j∗
B
p
Bp
Bp[
i
i
i])
如果购买张数超过限制就出队
代码
#include<iostream>
#include<cstdio>
using namespace std;
int n,t,w,mp,h,t1,q[2020],f[2020][2020],num[2020],a[2020],b[2020],c[2020],d[2020];
void buy(int i,int j)
{
while (h<t1&&q[t1-1]<f[i-w-1][j]+j*a[i]) t1--; //买的股票少剩下的钱反而少,不够优
q[t1]=f[i-w-1][j]+j*a[i];
num[t1++]=j; //入队(上面的j都是前文的k)
while (h==-1||(h<t1&&j-num[h]>c[i])) h++; //超过限制
f[i][j]=max(f[i][j],q[h]-j*a[i]); //前文的化简后的方程(这里的j和前文的j一样)
}
void sell(int i,int j)
{
while (h<t1&&q[t1-1]<f[i-w-1][j]+j*b[i]) t1--;
q[t1]=f[i-w-1][j]+j*b[i];
num[t1++]=j;
while (h==-1||(h<t1&&num[h]-j>d[i])) h++;
f[i][j]=max(f[i][j],q[h]-j*b[i]); //同上
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d%d%d",&t,&mp,&w);
for (int j=1;j<=t;j++)
scanf("%d%d%d%d",&a[j],&b[j],&c[j],&d[j]);
for (int j=0;j<=t;j++)
for (int k=0;k<=mp;k++)
f[j][k]=-1e8;
for (int j=1;j<=t;j++)
f[j][0]=0; //没有股票不花钱
for (int j=1;j<=w+1;j++)
for (int k=0;k<=c[j];k++) //只能买入
f[j][k]=-k*a[j];
f[0][0]=0;
for (int j=2;j<=t;j++)
{
for (int k=0;k<=mp;k++)
f[j][k]=max(f[j-1][k],f[j][k]); //不买不卖的最大值
if (j<=w+1) continue; //没到贸易时间
h=0,t1=0;
for (int k=0;k<=mp;k++)
buy(j,k); //买入
h=0,t1=0;
for (int k=mp;k>=0;k--)
sell(j,k); //卖出
}
int ans=-1e8;
for (int j=0;j<=mp;j++)
ans=max(ans,f[t][j]);
printf("%d\n",ans);
}
}