"Bzoj" "1855" "SCOI2010"/"Hdoj" "3401" stock trading

Source: Internet
Author: User

dp/Monotone Queue optimization

Puzzle: http://www.cnblogs.com/jianglangcaijin/p/3799736.html

F[I][J] indicates that, after the end of the day, the maximum profit of J shares is left in hand:

\[f[i][j]= \begin{cases} f[i-1][j] & &{(not buy or sell)}\\ f[i-w-1][k]-ap[i]* (j-k) & &{j-as[i] \leq k \leq j-1 (Buy)} \ f[i-w-1][k]+bp[i]* (k-j) & &{j+1 \leq k \leq J+bs[i] (Sell)} \end{cases} \]

For the buy, we will deform the equation to get:

$$ f[i][j]=f[i-w-1][k]+ap[i]*k-ap[i]*j $$

We know that the monotone queue optimization can be used to optimize the enumeration of k in the equation of the form of $ f[i]=max/min \{F[k] \}+g[i] $, in this equation, "F[k" is $ f[i-w-1][k]+ap[i]*k $, "G[i" is $-ap[ I]*j $, so we can do the maintenance of K while enumerating J (i.e. each f[i] is a DP under the monotonic queue optimization)

and selling the same.

1 /**************************************************************2 problem:18553 User:tunix4 language:c++5 result:accepted6 time:380 Ms7 memory:17068 KB8 ****************************************************************/9  Ten //Bzoj 1855 One#include <cmath> A#include <vector> -#include <cstdio> -#include <cstring> the#include <cstdlib> -#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) + #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + #definePB Push_back A using namespacestd; at intGetint () { -     intv=0, sign=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} -      while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} -     returnv*=Sign ; - } in Const intn= ., inf=~0u>>2; -typedefLong LongLL; to /******************tamplate*********************/ +   - structnode{ the     intx, y; *Nodeint_=0,int__=0): X (_), Y (__) {} $ }q[n];Panax Notoginseng intF[n][n]; - intMain () { the #ifndef Online_judge +Freopen ("1855.in","R", stdin); AFreopen ("1855.out","W", stdout); the #endif +     intN=getint (), M=getint (), w=getint (); -F (I,0, N) F (J,0, m) f[i][j]=-INF; $     intans=0, AP,BP, as, BS; $F (I,1, N) { -Ap=getint (); Bp=getint (); as=getint (); bs=getint (); -F (J,0, as) f[i][j]=-ap*J; theF (J,0, m) F[i][j]=max (f[i][j],f[i-1][j]); -         intk=i-w-1;Wuyi         if(k>=0){ the             intst=0, ed=0; -F (J,0, M) { Wu                  while(St<ed && q[st].x<j- as) st++; -                  while(St<ed && q[ed-1].Y&LT;=F[K][J]+AP*J) ed--; AboutQ[ed++]=node (j,f[k][j]+ap*j); $                 if(st<ed) F[i][j]=max (f[i][j],q[st].y-ap*j); -             } -St=ed=0; -D (J,m,0){ A                  while(st<ed && Q[st].x>j+bs) st++; +                  while(St<ed && q[ed-1].Y&LT;=F[K][J]+BP*J) ed--; theQ[ed++]=node (j,f[k][j]+bp*j); -                 if(st<ed) F[i][j]=max (f[i][j],q[st].y-bp*j); $             } the         } theAns=max (ans,f[i][0]); the     } theprintf"%d\n", ans); -     return 0; in}
View Code

"Bzoj" "1855" "SCOI2010"/"Hdoj" "3401" stock trading

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.