Hdu3401 trade monotonous queue optimized DP

Source: Internet
Author: User
Trade

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2918 accepted submission (s): 930

Problem descriptionrecently, 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 buy 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 sans BSI stocks.
Two trading days shocould have a interval of more than W days. that is to say, suppose you traded (any buy or shard 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?

Inputthe 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.

Outputthe most money lxhgww can earn.

Sample Input

15 2 02 1 1 12 1 1 13 2 1 14 3 1 15 4 1 1
 

Sample output

3
 

Authorlxhgww

Sourcehdoj monthly contest
-2010.05.01

Recommendlcy is wrong many times. First, we can conclude that DP [I] [J] = fmax (DP [I] [k] + AP [II] * (k-j ), DP [I] [k] + BP [II] * (k-j) DP [I] [J] table to find that J exists on day I, the complexity is too high. We can find that we can use the DP [I] [J] to get the maximum value one day first, so that we can reduce it to N ^ 3, we can set DP [I] [k] + BP [I] * K and DP [I] [k] + AP [I] [K], it can be optimized to N ^ 2 by saving a monotonous queue. However, you must initialize the nodes smaller than m separately. This is easy to make mistakes!

#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define MAXN 2005#define inf -1000000000int 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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.