hdu3401 Trade 单调队列优化dp

Trade

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

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
 

 

Recommend
lcy
这里错了好多次了,首先,我们可以得出dp[i][j]=fmax(dp[i][k]+ap[ii]*(k-j),dp[i][k]+bp[ii]*(k-j)) dp[i][j]表求第i天有j只,这样,复杂度太高了,我们可以发现,我们就可以通过dp[i][j]的先一天得到最大值,这样我们就可以减成n^3了,我们可以把dp[i][k]+bp[i]*k 和dp[i][k]+ap[i][k],用一个单调队列存起来,这样可以优化成了n^2,也就可以过了,但是要注意,这里,一定要把小于m的开始单独初始化,这里易错!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 2005
#define inf -1000000000
int ap[MAXN],bp[MAXN],as[MAXN],bs[MAXN],dp[MAXN][MAXN],queuea[MAXN],queueb[MAXN],maxp;
int fmax(int a,int b)
{
    if(a>b)
    return a;
    return b;
}
int ffa(int i,int k,int ii)
{
    return dp[i][k]+k*bp[ii];
}
int ffb(int i,int k,int ii)
{
    return dp[i][k]-(maxp-k)*ap[ii];
}
int main()
{
    int tcase,i,ii,j,k,n,m,s,e,l,r;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d%d",&n,&maxp,&m);
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
        }
        for(i=1;i<=m+1;i++)
            for(j=0;j<=maxp;j++)
            {
                dp[i][j]=(j<=as[i])?-ap[i]*j:inf;
                 if(i>1)dp[i][j]=fmax(dp[i][j],dp[i-1][j]);
            }
    for(ii=m+2;ii<=n;ii++)
    {
           i=ii-m-1;

            l=0;r=-1;
            for(j=0;j<=maxp;j++)
            {
                  while(l<=r&&ffb(i,j,ii)>ffb(i,queueb[r],ii))
                {
                    r--;
                }
                queueb[++r]=j;
                while(((j-queueb[l]>as[ii])))
                {
                    l++;
                }
                k=queueb[l];
                dp[ii][j]=fmax(dp[ii-1][j],dp[i][k]+ap[ii]*(k-j));
            }
           s=0;e=-1;                                                                                                                                                                                               ;

            for(j=maxp;j>=0;j--)
            {

                while(s<=e&&ffa(i,j,ii)>ffa(i,queuea[e],ii))
                {
                    e--;
                }
                queuea[++e]=j;
                while((queuea[s]-j>bs[ii]))
                {
                    s++;
                }
                k=queuea[s];
                dp[ii][j]=fmax(dp[ii][j],dp[i][k]+bp[ii]*(k-j));

            }


        }
        int maxx=dp[n][0];
        printf("%d\n",dp[n][0]);
    }

    return 0;
}

 

posted @ 2013-08-06 18:13  jlins  阅读(219)  评论(0编辑  收藏  举报