【HDU 3401】Trade

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
 Recommend
lcy   |   We have carefully selected several similar problems for you:  3400 3403 3404 3402 3415 
 
题意:拷贝来的:知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最多能赚多少钱。开始时有无限本金,要求任两次交易需要间隔W天以上,即第i天交易,第i+w+1天才能再交易。同时他任意时刻最多只能拥有maxp的股票,
题解:由于本人比较弱,单调队列优化版本暂时不会,先给出暴力(朴素DP)
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int N=2002;
const int oo=0x3f3f3f3f;
int t,p,w,mt_jjj;
int as[N],bs[N];
int ap[N],bp[N];
int dp[N][N];
//dp[i][j]表示第i天持j股最多的利润 
int main(){
    freopen("3401.in","r",stdin);
    freopen("3401.out","w",stdout);
    scanf("%d",&mt_jjj);
    while(mt_jjj--){
        int ans=0;
        memset(dp,-0x3f,sizeof(dp));
        scanf("%d %d %d",&t,&p,&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);
        //dp[i][j]=max{dp[i-1][j],max{dp[r][k]-APi[i]*(j-k)}(0<r<i-w,k<j),max{dp[r][k]+BPi[i]*(k-j)}(0<r<i-w,k>j)}
        for(int i=2;i<=t;i++){//枚举第i天
            for(int j=0;j<=p;j++)
                dp[i][j]=max(dp[i-1][j],dp[i][j]);            
                if(i<=w+1) continue;
            for(int j=0;j<=p;j++){//手持j股  
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
                for(int r=0;r<i-w;r++)//第i天是由第r天推来的 
                    for(int k=j;j-k<=as[i];k--)//买入股数 
                        dp[i][j]=max(dp[i][j],dp[i-w-1][k]-ap[i]*(j-k));
                for(int r=0;r<i-w;r++)//第i天是由第r天推来的 
                    for(int k=j;k-j<=bs[i];k++)//卖出股数 
                        //cout<<dp[r][k]<<endl;
                        dp[i][j]=max(dp[i][j],dp[i-w-1][k]+bp[i]*(k-j));
                //printf("%d %d %d
",c1,c2,c3);
            }
        } 
        for(int i=0;i<=p;i++)
            ans=max(ans,dp[t][i]);
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11336330.html