Bzoj 1855 stock Monotone queue optimization DP

Source: Internet
Author: User

1855: [Scoi2010] Stock trading time limit:5 Sec Memory limit:64 MB
submit:909 solved:420
[Submit] [Status] [Discuss] Description recently Lxhgww and fascinated by the investment stock, through a period of observation and study, he summed up the stock market some rules. Through a period of observation, Lxhgww predicted the future of a stock trend in the next T days, the stock bid price for the first day of each share of the API, the first day of the stock price per share of BPi (data assurance for each I, there is API>=BPI), but not unlimited trading every day, As a result, the stock Exchange stipulates that one-time purchase of the first day can buy only ASI shares, and at most one sell only the BSI shares. In addition, the stock Exchange has enacted two regulations. In order to avoid the crazy trade, the stock exchange stipulated that in two transactions (one day to buy or sell is a transaction), at least at the interval of W days, that is, if the transaction occurred on the first day, then from the i+1 day to the i+w days, no transaction can occur. At the same time, in order to avoid monopoly, the stock Exchange also stipulates that at any one person's hand the number of shares cannot exceed Maxp. Before the 1th day, LXHGWW had a large amount of money (which could be considered unlimited), but there was no stock, and of course, T days later, LXHGWW wanted to make the most money, smart handlers, can you help him? Input data The first row contains 3 integers, respectively, t,maxp,w. Next T line, line I represents the stock movement of the i-1 day, 4 integers per line, respectively, representing API,BPI,ASI,BSI. Output data is a row, including 1 digits, representing the maximum amount of money lxhgww can earn. Sample Input5 2 0
2 1 1 1
2 1 1 1
3 2 1 1
4 3 1 1
5 4 1 1
Sample Output3
HINT

For 30% of data, 0 < =w for 50% data, 0 < =w for 100% data, 0 < =w
For all data, 1 < =BPI < =api < =1000,1 < =ASI,BSI < =MAXP

We set DP[I][J] to indicate that the first day of the hand has J shares of the maximum profit, easy to write equations

Buy: dp[i][j]=max{dp[i-w-1][k]-ap[i]* (j-k)};

Sell: dp[i][j]=max{dp[i-w-1][k]+bp[i]* (K-J)};

Nothing:dp[i][j]=max (Dp[i][j],dp[i-1][j]);

In the section of optimization, use buy Example:

Deformation: dp[i][j]=max{dp[i-w-1][k]+ap[i]*k-ap[i]*j};

Because each i has a unique ap[i], so maintain two tuples (k,dp[i-w-1][k]+ap*k)

It is not necessary to enumerate K, the complexity is acceptable.

Selling the same.

Nothing can be updated directly.

Because in the best case there is no stock, so directly with Dp[i][0] update the best solution on the line.

So this is the end of the question ...

( say Rank1 god ben Run more than 100 ms, my is 500ms is what situation )

/************************************************************** problem:1855 user:201484348 language:c++ result:accepted time:500 Ms memory:16928 kb****************************************************************/#include<iostream>#include<cstdio>#include<cstring>using namespacestd;Const intn=2001;Const intinf=0x3f3f3f3f;structnode{intx, y;} Q[n];intN,dp[n][n],w,p;inlineintRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}intMain () {n=read (); P=read (), w=read (); intAP,BP, as, BS; intans=-INF; Memset (DP,-inf,sizeof(DP));  for(intI=1; i<=n;i++) {AP=read (), Bp=read (), as=read (), bs=read ();  for(intj=0; j<= as; j + +) dp[i][j]=-ap*J;  for(intj=0; j<=p;j++) Dp[i][j]=max (dp[i][j],dp[i-1][j]); intk=i-w-1; if(k>=0)        {            intL=0, r=0;  for(intj=0; j<=p;j++)            {                 while(l<r&&q[l].x<j- as) l++;  while(l<r&&q[r-1].Y&LT;=DP[K][J]+AP*J) r--; Q[r++]= (node) {j,dp[k][j]+ap*J}; if(l<r) Dp[i][j]=max (dp[i][j],q[l].y-ap*j); } l=0, r=0;  for(intj=p;j>=0; j--)            {                 while(L&LT;R&AMP;&AMP;Q[L].X&GT;J+BS) l++;  while(l<r&&q[r-1].Y&LT;=DP[K][J]+BP*J) r--; Q[r++]= (node) {j,dp[k][j]+bp*J}; if(l<r) Dp[i][j]=max (dp[i][j],q[l].y-bp*j); }} ans=max (ans,dp[i][0]); } printf ("%d", ans);}

Bzoj 1855 stock Monotone queue optimization DP

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.