洛谷 [SCOI2010]股票交易 | 单调性DP

题目链接

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 2005
using namespace std;
int n,q[N],ap,bp,dp[N][N],as,bs,m,w;
int main()
{
    scanf("%d%d%d",&n,&m,&w);
    memset(dp,128,sizeof(dp));
    for (int i=1;i<=n;i++)
    {
    scanf("%d%d%d%d",&ap,&bp,&as,&bs);
    for (int j=0;j<=as;j++) dp[i][j]=-1*j*ap;
    for (int j=0;j<=m;j++) dp[i][j]=max(dp[i][j],dp[i-1][j]);
    if (i<=w) continue;
    int l=1,r=0;
    for (int j=0;j<=m;j++)
    {
        while (l<=r && q[l]<j-as) l++;
        while (l<=r && dp[i-w-1][q[r]]+q[r]*ap<=dp[i-w-1][j]+j*ap) r--;
        q[++r]=j;
        if (l<=r) dp[i][j]=max(dp[i][j],dp[i-w-1][q[l]]+q[l]*ap-j*ap);
    }
    l=1,r=0;
    for (int j=m;j>=0;j--)
    {
        while (l<=r && q[l]>j+bs) l++;
        while (l<=r && dp[i-w-1][q[r]]+q[r]*bp<=dp[i-w-1][j]+j*bp) r--;
        q[++r]=j;
        dp[i][j]=max(dp[i][j],dp[i-w-1][q[l]]+q[l]*bp-j*bp);
    }
    }
    printf("%d\n",dp[n][0]);
    return 0;
}

 

posted @ 2018-02-23 08:35  MSPqwq  阅读(134)  评论(0编辑  收藏  举报