Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3853 Accepted Submission(s): 1248
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?
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.
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
题解:容易写出DP方程 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)} 分别是第i天不交易,买股票和卖股票3种转移。
第一眼看会发现这个方程十分冗杂,没有下手的地方。我们可以分析下,对于买卖股票的转移中的dp[r][k]中的r是否有枚举的价值,注意dp[i][j]中取最大时会考虑dp[i-1][j]的值,也就是dp[i-w-1][k]>=dp[r][k](0<r<i-w)恒成立。这样就省去了r 的枚举,但这还不够。以买股票为例,我们可以化简max{dp[i-w-1][k]-APi[i]*(j-k)}(k<j)},即dp[i-w-1][k]-APi[i]*(j-k)=dp[i-w-1][k]+APi[i]*k-APi[i]*j(k<j),可以发现对于确定的i和j只要取符合条件中最大的dp[i-w-1][k]+APi[i]*k即可,这个可以通过单调队列实现。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
int x,p;
}qu[2010],temp;
int T,t,n,m,MaxP,w,INF=1e9;
int dp[2010][2010],AP[2010],BP[2010],AS[2010],BS[2010],head,tail;
int main()
{
int i,j,k,ans;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%d%d%d",&n,&MaxP,&w);
w++;
for(i=1;i<=n;i++)
scanf("%d%d%d%d",&AP[i],&BP[i],&AS[i],&BS[i]);
for(i=0;i<=n;i++)
for(j=0;j<=MaxP;j++)
dp[i][j]=-INF;
for(i=0;i<=w;i++)
for(j=0;j<=AS[i];j++)
dp[i][j]=-AP[i]*j;
for(i=2;i<=n;i++)
{
for(j=0;j<=MaxP;j++)
dp[i][j]=max(dp[i][j],dp[i-1][j]);
if(i<=w)
continue;
head=1;tail=0;
for(j=0;j<=MaxP;j++)
{
temp.x=dp[i-w][j]+AP[i]*j;
temp.p=j;
while(head<=tail && qu[tail].x<temp.x)
tail--;
qu[++tail]=temp;
while(head<tail && qu[head].p+AS[i]<j)
head++;
dp[i][j]=max(dp[i][j],qu[head].x-AP[i]*j);
}
head=1;tail=0;
for(j=MaxP;j>=0;j--)
{
temp.x=dp[i-w][j]+BP[i]*j;
temp.p=j;
while(head<=tail && qu[tail].x<temp.x)
tail--;
qu[++tail]=temp;
while(head<tail && qu[head].p>j+BS[i])
head++;
dp[i][j]=max(dp[i][j],qu[head].x-BP[i]*j);
}
}
ans=0;
for(i=0;i<=MaxP;i++)
ans=max(ans,dp[n][i]);
printf("%d\n",ans);
}
}