hdu 3401 Trade dp+單調隊列

Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3599    Accepted Submission(s): 1167


Problem Description
Recently, lxhgww is addicted to stock, he finds some regular patterns after a few days' study.
He forecasts the next T days' stock market. On the i'th day, you can buy one stock with the price APi or sell one stock to get BPi. 
There are some other limits, one can buy at most ASi stocks on the i'th day and at most sell BSi stocks.
Two trading days should have a interval of more than W days. That is to say, suppose you traded (any buy or sell stocks is regarded as a trade)on the i'th day, the next trading day must be on the (i+W+1)th day or later.
What's more, one can own no more than MaxP stocks at any time.

Before the first day, lxhgww already has infinitely money but no stocks, of course he wants to earn as much money as possible from the stock market. So the question comes, how much at most can he earn?
 

Input
The first line is an integer t, the case number.
The first line of each case are three integers T , MaxP , W .
(0 <= W < T <= 2000, 1 <= MaxP <= 2000) .
The next T lines each has four integers APi,BPi,ASi,BSi( 1<=BPi<=APi<=1000,1<=ASi,BSi<=MaxP), which are mentioned above.
 

Output
The most money lxhgww can earn.
 

Sample Input
1 5 2 0 2 1 1 1 2 1 1 1 3 2 1 1 4 3 1 1 5 4 1 1
 

Sample Output
3
 

Author
lxhgww
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3400 3403 3404 3402 3415 
 

單調隊列做的題太少

此題參照博客http://blog.csdn.net/acm_ted/article/details/7909742的思路。

思路講的非常清楚了,以後單調隊列就這麼寫了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N=2005;
const int inf=0x3f3f3f3f;
int AP[N],BP[N],AS[N],BS[N],dp[N][N];
int front,back;
struct node
{
    int x;
    int p;
}q[2005],tmp;
int main()
{
    int t,T,W,MaxP;
    scanf("%d",&t);
    while(t--){
        memset(dp,-inf,sizeof(dp));
        scanf("%d%d%d",&T,&MaxP,&W);
        for(int i=1;i<=T;i++)
            scanf("%d%d%d%d",&AP[i],&BP[i],&AS[i],&BS[i]);
        for(int i=1;i<=W+1;++i)
            for(int j=0;j<=AS[i];++j)
                dp[i][j]=(-AP[i]*j);
        for(int i=2;i<=T;++i){
            for(int j=0;j<=MaxP;++j)
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
            if(i<=W+1)continue;
            front=back=1;
            for(int j=0;j<=MaxP;++j){
                tmp.p=j;
                tmp.x=dp[i-W-1][j]+AP[i]*j;
                while(front<back&&q[back-1].x<tmp.x)back--;
                q[back++]=tmp;
                while(front<back&&q[front].p+AS[i]<j)front++;
                dp[i][j]=max(dp[i][j],q[front].x-AP[i]*j);
            }
            front=back=1;
            for(int j=MaxP;j>=0;--j){
                tmp.p=j;
                tmp.x=dp[i-W-1][j]+BP[i]*j;
                while(front<back&&q[back-1].x<tmp.x)back--;
                q[back++]=tmp;
                while(front<back&&q[front].p-BS[i]>j)front++;
                dp[i][j]=max(dp[i][j],q[front].x-BP[i]*j);
            }
        }
        int ans=0;
        for(int i=0;i<=MaxP;++i)
            ans=max(ans,dp[T][i]);
        printf("%d\n",ans);
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章