[SCOI 2010] 股票交易

[题目链接]

         https://www.lydsy.com/JudgeOnline/problem.php?id=1855

[算法]

        单调队列优化动态规划

[代码]

       

#include<bits/stdc++.h>
using namespace std;
#define MAXT 2010

struct info
{
        int pos,val;
} ;

int i,j,ans,T,MaxP,W;
deque< info > q;
int AP[MAXT],BP[MAXT],AS[MAXT],BS[MAXT];
int f[MAXT][MAXT];

int main() 
{
        
        scanf("%d%d%d",&T,&MaxP,&W);
        for (i = 1; i <= T; i++) scanf("%d%d%d%d",&AP[i],&BP[i],&AS[i],&BS[i]);
        memset(f,128,sizeof(f));
        for (i = 0; i <= T; i++) f[i][0] = 0;
        for (i = 1; i <= T; i++)
        {
                for (j = 0; j <= AS[i]; j++) f[i][j] = max(f[i][j],-1 * AP[i] * j);
                for (j = 0; j <= MaxP; j++) f[i][j] = max(f[i][j],f[i - 1][j]);
                if (i - W - 1 >= 0)
                {
                        q.clear();
                        for (j = 0; j <= MaxP; j++)
                        {
                                while (!q.empty() && q.front().pos < j - AS[i]) q.pop_front();
                                while (!q.empty() && f[i - W - 1][j] + j * AP[i] >= q.back().val) q.pop_back();
                                q.push_back((info){j,f[i - W - 1][j] + j * AP[i]});
                                f[i][j] = max(f[i][j],q.front().val - j * AP[i]);
                        }
                        q.clear();
                        for (j = MaxP; j >= 0; j--)
                        {
                                while (!q.empty() && q.front().pos > j + BS[i]) q.pop_front();
                                while (!q.empty() && f[i - W - 1][j] + j * BP[i] >= q.back().val) q.pop_back();
                                q.push_back((info){j,f[i - W - 1][j] + j * BP[i]});
                                f[i][j] = max(f[i][j],q.front().val - j * BP[i]);
                        }
                }
        }
        for (i = 0; i <= MaxP; i++) ans = max(ans,f[T][i]);
        printf("%d\n",ans);
        
        return 0;
    
}

 

posted @ 2018-07-23 18:35  evenbao  阅读(138)  评论(0编辑  收藏  举报