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.
思路
在不使用单调队列的情况下:
d
p
[
i
]
[
j
]
=
m
a
x
(
d
p
[
i
−
1
]
[
j
]
,
m
a
x
(
d
p
[
r
]
[
k
]
−
A
P
i
[
i
]
∗
(
j
−
k
)
)
(
0
<
r
<
i
−
w
,
k
<
j
)
,
m
a
x
(
d
p
[
r
]
[
k
]
+
B
P
i
[
i
]
∗
(
k
−
j
)
)
(
0
<
r
<
i
−
w
,
k
>
j
)
)
dp[i][j]=max(dp[i-1][j],max(dp[r][k]-APi[i]*(j-k))(0<r<i-w,k<j),max(dp[r][k]+BPi[i]*(k-j))(0<r<i-w,k>j))
dp[i][j]=max(dp[i−1][j],max(dp[r][k]−APi[i]∗(j−k))(0<r<i−w,k<j),max(dp[r][k]+BPi[i]∗(k−j))(0<r<i−w,k>j))
我们发现
m
a
x
(
d
p
[
r
]
[
k
]
−
A
P
i
[
i
]
∗
(
j
−
k
)
)
(
0
<
r
<
i
−
w
,
k
<
j
)
,
m
a
x
(
d
p
[
r
]
[
k
]
+
B
P
i
[
i
]
∗
(
k
−
j
)
)
(
0
<
r
<
i
−
w
,
k
>
j
)
max(dp[r][k]-APi[i]*(j-k))(0<r<i-w,k<j),max(dp[r][k]+BPi[i]*(k-j))(0<r<i-w,k>j)
max(dp[r][k]−APi[i]∗(j−k))(0<r<i−w,k<j),max(dp[r][k]+BPi[i]∗(k−j))(0<r<i−w,k>j)
可以单调队列优化,所以我们就优化一下吧
code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<deque>
#include<cstdio>
#include<map>
using namespace std;
int ap[2001],bp[2001],as[2001],bs[2001];
int p[2001];
int f[2001][2001],s[2001];
int t,w,r,c,b,x,l=1,j,k,L,R;
int n,maxp,u,i,m,ans=-0x7f7f7f7f;
void read(int& x)
{
x=0;
unsigned int f=1;
char ch=getchar();
while (!isdigit(ch)) (ch=='-')&&(f=-1),ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x*=f;
}
void wr(int x)
{
(x<0)&&(x=-x,putchar('-'));
if (x>9) wr(x/10);
putchar(x%10^48);
}
int main()
{
read(t);
while (t--)
{
read(n),read(maxp),read(w);
for (i=1;i<=n;i++) read(ap[i]),read(bp[i]),read(as[i]),read(bs[i]);
memset(f,0xcf,sizeof(f));
ans=0;
for (i=1;i<=w+1;i++)
{
for (j=0;j<=as[i];j++)
{
f[i][j]=-ap[i]*j;//初始化
}
}
for (i=2;i<=n;i++)
{
for (j=0;j<=maxp;j++) f[i][j]=max(f[i][j],f[i-1][j]);
if (i<=w+1) continue;//显然这个时候买不了也卖不掉
l=r=1;
//买
for (j=0;j<=maxp;j++)//枚举件数
{
while (l<r&&s[r-1]<f[i-w-1][j]+ap[i]*j) r--;//dp[i-w+1][j]为w天前容量为j的价值,加上ap[i]*j即再买j件
s[r]=f[i-w-1][j]+ap[i]*j;
p[r++]=j;
while (l<r&&p[l]+as[i]<j) l++;//显然如果之前买的某个件数比现在要买的多出的部分超限,当然就无法留在候选集合里了,有人会说这样候选集合里还有别的不符合条件的值,但是我们目前只需要利用最值(话说好像也不会有)
f[i][j]=max(f[i][j],s[l]-ap[i]*j);
}
l=r=1;
//卖
for (j=maxp;j>=0;j--)//枚举件数
{
while (l<r&&s[r-1]<f[i-w-1][j]+bp[i]*j) r--;//dp[i-w+1][j]为w天前容量为j的价值,加上bp[i]*j即再卖j件
s[r]=f[i-w-1][j]+bp[i]*j;
p[r++]=j;
while (l<r&&p[l]-bs[i]>j) l++;//显然如果之前卖的某个件数比现在要卖的多出的部分超限,当然就无法留在候选集合里了,有人会说这样候选集合里还有别的不符合条件的值,但是我们目前只需要利用最值(话说好像也不会有x2)
f[i][j]=max(f[i][j],s[l]-bp[i]*j);
}
}
for (i=0;i<=maxp;i++) ans=max(ans,f[n][i]);
wr(ans);
printf("\n");
}
return 0;
}