Trade

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


题目分析:

主要思想是动态规划:dp[i][j]表示第i天手上有j支股票时的最大盈利

这样可以很快建立状态转移方程:dp[i][j]=max{dp[i-1][j],dp[i-w-1][k]-api[i]*(j-k)}   (0<k<j) 这是买进股票时的方程,

卖出时与之类似 ,这样便是O(n^3)的算法,显然会超时,于是,便需要单调队列优化来将复杂度将为O(n^2)。

现在仅考虑买进股票的情况:

第i天不买股票时,直接等于dp[i-1][j]

第i天买股票是,dp[i][j]=dp[i-w-1][k]+api[i]*(j-k)  方程变形的 dp[i][j]=dp[i-w-1][k]+api[i]*k-api[i];  

这样就转换成:dp[i]=max(f[k]) + g[i]  k<i && g[i]是与k无关的变量)的形式,便可以

                            使用单调队列进行优化,具体操作见代码。

代码:

# include<stdio.h>
# include<string.h>
# define max(a,b) a>b?a:b
# define inf -0xfffffff
int dp[2005][2005],api[2005],bpi[2005],asi[2005],bsi[2005];
struct node
{
    int v,p;
}q[2005];
int main()
{
    int t,n,maxp,w,i,j;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d%d",&n,&maxp,&w);
        for (i=1;i<=n;i++) scanf("%d%d%d%d",&api[i],&bpi[i],&asi[i],&bsi[i]);
        for (i=0;i<=n;i++)
        for (j=0;j<=maxp;j++) dp[i][j]=inf;
        for (i=1;i<=w;i++)
        for (j=0;j<=maxp&&j<=asi[i];j++)
        dp[i][j]=-api[i]*j;
        dp[0][0]=0;
        for (i=1;i<=n;i++)
        {
            for (j=0;j<=maxp;j++)
            {
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
            }
            if (i<w+1) continue;
            int head,tail;
            head=tail=0;
            for (j=0;j<=maxp;j++)   //买进时从小到大,i时已经存储了0~i-1的信息
            {
                int now=dp[i-w-1][j]+j*api[i];    // 正如题目分析中求max(f[k])的过程
                while (head<tail&&q[tail-1].v<now) tail--;
                q[tail].v=now; q[tail++].p=j;
                while (head<tail&&q[head].p+asi[i]<j) head++;
                dp[i][j]=max(dp[i][j],q[head].v-j*api[i]);  //此处的j和上面的j不一样,正好利用两者之间的差值构造出了i-k
            }
            head=tail=0;             //卖出时的形式和上面类似
            for (j=maxp;j>=0;j--)  //卖出时从大到小,i时已经存储了i+1~maxp的信息
            {
                int now=dp[i-w-1][j]+j*bpi[i];
                while (head<tail&&q[tail-1].v<now) tail--;
                q[tail].v=now; q[tail++].p=j;
                while (head<tail&&q[head].p-bsi[i]>j) head++;
                dp[i][j]=max(dp[i][j],q[head].v-j*bpi[i]);
            }
        }
        int ans=0;
        for (i=0;i<=maxp;i++)
        ans=max(ans,dp[n][i]);
        printf("%d\n",ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值