#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 2010
using namespace std;
int t,maxp,w;
int f[N][N];
int q[N];
struct node
{
int ap,bp,as,bs;
}a[N];
int main()
{
scanf("%d%d%d",&t,&maxp,&w);
for(int i=1;i<=t;i++)
{
scanf("%d%d%d%d",&a[i].ap,&a[i].bp,&a[i].as,&a[i].bs);
}
memset(f,-0x3f,sizeof(f));
for(int i=1;i<=t;i++)
{
for(int j=0;j<=a[i].as;j++)f[i][j]=-a[i].ap*j;
for(int j=0;j<=maxp;j++)f[i][j]=max(f[i][j],f[i-1][j]);
if(i-w-1>=0)
{
int head=0,tail=0;
for(int j=0;j<=maxp;j++)
{
while(head<tail&&q[head]<j-a[i].as)head++;
while(head<tail&&f[i-w-1][j]+j*a[i].ap>=f[i-w-1][q[tail-1]]+q[tail-1]*a[i].ap)tail--;
q[tail++]=j;
if(head<tail)f[i][j]=max(f[i][j],f[i-w-1][q[head]]-(j-q[head])*a[i].ap);
}
head=0,tail=0;
for(int j=maxp;j>=0;j--)
{
while(head<tail&&q[head]>j+a[i].bs)head++;
while(head<tail&&f[i-w-1][j]+j*a[i].bp>=f[i-w-1][q[tail-1]]+q[tail-1]*a[i].bp)tail--;
q[tail++]=j;
if(head<tail)f[i][j]=max(f[i][j],f[i-w-1][q[head]]+(q[head]-j)*a[i].bp);
}
}
}
int ans=0;
for(int i=0;i<=maxp;i++)
{
ans=max(ans,f[t][i]);
}
printf("%d\n",ans);
}
BZOJ 1855 [Scoi2010]股票交易 单调队列优化DP
原文地址:http://blog.csdn.net/wzq_qwq/article/details/46410395