HDU 3401 Trade(dp+单调队列优化)

Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5843    Accepted Submission(s): 2013


 

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

题意:一个人买股票,有n天,每天买可以最多买as[i]只股票,每只花费ap[i],每天最多卖bs[i]只股票,每只可以卖bp[i],在任何时刻手里的股票数目最多不能超过m只,并且必须间隔天数大于等于w+1才能交易.问你这个人n天后最多赚多少钱。所有数据<=2000。

思路:显然是dp。设dp[i][j]表示第i天持有j只股票的最大利润,

那么我们可以得到买和卖的两个状态转移方程:

买:

dp[i][j]=max(dp[i][j],dp[i-w-1][k]-ap[i]*(j-k)),j\geqslant k

卖:

dp[i][j]=max(dp[i][j],dp[i-w-1][k]+bp[i]*(k-j)),k\geqslant j

对于任意一天,有:

dp[i][j]=max(dp[i-1][j],dp[i][j])

对于前w+1天,有

dp[i][j]=-j*ap[i]

对于第0天,有dp[0][0]=0。

但是我们要for i ,j,k的话时间复杂度就不够了。

注意到,买的状态转移方程可以写成

dp[i][j]=max(dp[i][j],dp[i-w-1][k]+ap[i]*k-ap[i]*j),j\geqslant k

同样,卖的状态转移方程可以写成

dp[i][j]=max(dp[i][j],dp[i-w-1][k]+bp[i]*k-bp[i]*j),k\geqslant j

显然枚举j时,每次都是从最大的

dp[i-w-1][k]+ ap[i]*k,dp[i-w-1][k]+ bp[i]*k  转移过来。于是就可以用单调队列优化了。

注意两个方程成立的条件,买要从小到大dp,卖要从大到小dp。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=2050;
int as[maxn],bs[maxn],ap[maxn],bp[maxn];
int n,m,k,w;
int dp[maxn][maxn];
struct node
{
    int dt;
    int v;
};
node q[maxn];
int head,tail;
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&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<=m;j++)
        dp[i][j]=-inf;
        for(int i=1;i<=w+1;i++)
        for(int j=0;j<=min(m,as[i]);j++)
        dp[i][j]=-ap[i]*j;
        dp[0][0]=0;
        for(int i=1;i<=n;i++)
        {
            head=tail=0;
            for(int j=0;j<=m;j++)
            {
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
                if(i-w-1<=0) continue;
                int k=i-w-1;
                int vv=dp[k][j]+ap[i]*j;
                while(head<tail&&q[tail-1].v<vv) tail--;
                q[tail].dt=j;q[tail++].v=vv;
                while(head<tail&&q[head].dt+as[i]<j) head++;
                dp[i][j]=max(dp[i][j],q[head].v-ap[i]*j);
            }
            head=tail=0;
            for(int j=m;j>=0;j--)
            {
                if(i-w-1<=0) continue;
                int k=i-w-1;
                int vv=dp[k][j]+bp[i]*j;
                while(head<tail&&q[tail-1].v<vv) tail--;
                q[tail].dt=j;q[tail++].v=vv;
                while(head<tail&&q[head].dt-bs[i]>j) head++;
                dp[i][j]=max(dp[i][j],q[head].v-bp[i]*j);
            }
        }
        int ans=0;
        for(int i=0;i<=m;i++)
        ans=max(ans,dp[n][i]);
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LSD20164388/article/details/83002355