bzoj1855: [Scoi2010]股票交易
码了一下午,然后被一群sxD成傻逼(被mayaohua大佬支配恐惧)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int inf=99999999; int ap[2100],bp[2100],as[2100],bs[2100]; int f[2100][2100],head,tail,q[2100],id[2100]; int main() { int n,m,w; scanf("%d%d%d",&n,&m,&w); for (int i=1;i<=n;i++) scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]); for(int i=1;i<=m;i++)f[0][i]=-inf; for(int i=1;i<=n;i++) { head=1;tail=0; id[1]=0; for(int j=0;j<=m;j++) { f[i][j]=f[i-1][j]; if(i<=w) { if (j<=as[i])f[i][j]=max(f[i][j],-j*ap[i]); continue; } while(head<=tail&&id[head]<j-as[i])head++; if(head<=tail)f[i][j]=max(f[i][j],q[head]-j*ap[i]); while(head<=tail&&q[tail]<f[i-w-1][j]+j*ap[i])tail--; q[++tail]=f[i-w-1][j]+j*ap[i]; id[tail]=j; } head=1;tail=0; id[1]=0; if(i>w) { for(int j=m;j>=0;j--) { while(head<=tail&&id[head]>j+bs[i])head++; if(head<=tail)f[i][j]=max(f[i][j],q[head]-j*bp[i]); while(head<=tail&&q[tail]<f[i-w-1][j]+j*bp[i])tail--; q[++tail]=f[i-w-1][j]+j*bp[i]; id[tail]=j; } } } printf("%d",f[n][0]); return 0; }
pain and happy in the cruel world.