HDOJ 3401 Trade(单调队列优化DP)

本文介绍了一种基于动态规划的股票交易策略算法,通过优化的状态转移方程来解决具有特定限制条件下的最大收益问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Trade

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

Problem Description

Recently, lxhgww is addictedto 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 onestock 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 andat 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'thday, 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, ofcourse he wants to earn as much money as possible from the stock market. So thequestion comes, how much at most can he earn?

 

 

Input

The first line is an integert, 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
BPiASiBSi(1<=BPi<=APi<=1000,1<=ASi,BSi<=MaxP), which are mentioned above.

 

 

Output

The most money lxhgww canearn.

 

 

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

题意:一个人知道接下来n天的股市行情,而且他每天可以进行买入和卖出操作,问最终他最多能赚到多少钱

用dp[i][j]表示第i 天拥有j只股票的时候,赚了多少钱

状态转移方程为:

1.从前一天不买不卖:

          dp[i][j]=max(dp[i-1][j],dp[i][j])

2.从前i-w-1天买入一些股票:

           dp[i][j]=max(dp[i-w-1][k]-(j-k)*ap[i],dp[i][j])      //k为前一状态所拥有的股票数目

3、从前i-W-1天卖掉一些股票:

           dp[i][j]=max(dp[i-w-1][k]+(k-j)*bp[i],dp[i][j])

注:有人可能会问为什么只要考虑前i-w-1天的买入和卖出即可?原因是前i-w-2天可以通过不买不卖将自己的最优状态转移到i-w-1天,以此类推

考虑到时限的因素,想到用优先队列优化

买入时: dp[i][j]=max(dp[i-w-1][k]+k*ap[i])-j*ap[i]。令f[i-w-1][k]=dp[i-w-1][k]+k*ap[i],则dp[i][j]=max(f[i-w-1][k]) - j*bp[i]。

卖出时: dp[i][j]=max(dp[i-w-1][k]+k*ap[i])-j*bp[i]。令f[i-w-1][k]=dp[i-w-1][k]+k*bp[i],则dp[i][j]=max(f[i-w-1][k]) - j*bp[i]。


#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<=(b);++i)
const int maxn = 2005;
const int mod = 9973;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define ll long long
#define rush() int T;scanf("%d",&T);while(T--)
struct node
{
    int pos;
    int f;
}q[maxn];
int ap[maxn],bp[maxn],as[maxn],bs[maxn];
int dp[maxn][maxn];
int main()
{
    int n,p,w;
    rush()
    {
        scanf("%d%d%d",&n,&p,&w);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
        }
        for(int i=0;i<=n;i++)
            for(int j=0;j<=p;j++)
                dp[i][j]=-INF;
        for(int i=1;i<=w+1;i++)           //初始化w周期内买股情况
            for(int j=0;j<=min(p,as[i]);j++)
                dp[i][j]=-ap[i]*j;
        dp[0][0]=0;
        int head,tail;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=p;j++)         //不买不卖
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
            if(i<=w+1)
                continue;
            int pre=i-w-1;                 //队列中保存的是第i-w-1天的信息
            head=tail=0;
            for(int j=0;j<=p;j++)          //买入
            {
                int cnt=dp[pre][j]+j*ap[i];
                while(head<tail&&q[tail-1].f<cnt)   //单调队列求max值
                    tail--;
                q[tail].f=cnt;
                q[tail++].pos=j;
                while(head<tail&&q[head].pos+as[i]<j)  //每天买入数量的限制
                    head++;
                dp[i][j]=max(dp[i][j],q[head].f-j*ap[i]);
            }
            head=tail=0;
            for(int j=p;j>=0;j--)           //卖出
            {
                int cnt=dp[pre][j]+j*bp[i];
                while(head<tail&&q[tail-1].f<cnt)
                    tail--;
                q[tail].f=cnt;
                q[tail++].pos=j;
                while(head<tail&&q[head].pos-bs[i]>j)
                    head++;
                dp[i][j]=max(dp[i][j],q[head].f-j*bp[i]);
            }
        }
        int ans=0;
        for(int i=0;i<=p;i++)
            ans=max(ans,dp[n][i]);
        printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值