【BZOJ】[Scoi2010] 股票交易
阿新 • • 發佈:2018-12-20
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 7 #define For(i,a,b) for(register int i=a;i<=b;++i) 8 #define Dwn(i,a,b) for(register int i=a;i>=b;--i) 9 #define Re register 10 11 using namespacestd; 12 13 const int N=2e3+10; 14 int f[N][N],q[N*2],qf,qr; 15 int n,m,As[N],Ap[N],Bs[N],Bp[N],W,T,MaxP; 16 inline void read(int &v){ 17 v=0; 18 char c=getchar(); 19 while(c<'0'||c>'9')c=getchar(); 20 while(c>='0'&&c<='9')v=v*10+c-'0',c=getchar(); 21 } 22int main(){ 23 read(T); read(MaxP); read(W); 24 For(i,1,T){ 25 read(Ap[i]); read(Bp[i]); 26 read(As[i]); read(Bs[i]); 27 } 28 memset(f,128,sizeof(f)); 29 For(i,1,T){ 30 // I bought some stocks from nowhere 31 For(j,0,As[i]){ 32 f[i][j]=-1*j*Ap[i]; 33 } 34 // I didn't buy anything 35 For(j,0,MaxP){ 36 f[i][j]=max(f[i][j],f[i-1][j]); 37 } 38 39 if(i-W-1<=0)continue; 40 41 // I bought some stock today after W days 42 qf=1; qr=0; 43 For(j,0,MaxP){ 44 while(qf<=qr&&q[qf]<j-As[i])qf++; 45 if(qf<=qr){ 46 int k=q[qf]; 47 f[i][j]=max(f[i][j],f[i-W-1][k]+k*Ap[i]-j*Ap[i]); 48 } 49 while(qf<=qr&&f[i-W-1][q[qr]]+q[qr]*Ap[i]<=f[i-W-1][j]+j*Ap[i])qr--; 50 q[++qr]=j; 51 } 52 53 // I sold some stock today after W days 54 qf=1; qr=0; 55 Dwn(j,MaxP,0){ 56 while(qf<=qr&&q[qf]>j+Bs[i])qf++; 57 if(qf<=qr){ 58 int k=q[qf]; 59 f[i][j]=max(f[i][j],f[i-W-1][k]+k*Bp[i]-j*Bp[i]); 60 } 61 while(qf<=qr&&f[i-W-1][q[qr]]+q[qr]*Bp[i]<=f[i-W-1][j]+j*Bp[i])qr--; 62 q[++qr]=j; 63 } 64 } 65 int fn=-2147483600; 66 For(i,0,MaxP)fn=max(fn,f[T][i]); 67 cout<<fn<<endl; 68 return 0; 69 }