您的位置:首页 > 其它

hdu 3401 Trade(DP+单调队列优化)

2012-08-18 16:42 501 查看


Trade

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2118 Accepted Submission(s): 665



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

HDOJ Monthly Contest – 2010.05.01



Recommend

lcy


题目:http://acm.hdu.edu.cn/showproblem.php?pid=3401

题意:告诉你T天里,物品的买卖价格,和最多买卖数量,还有T天里能买入最多的物品数量,每一次买卖间隔时间必须大于w+1

分析:这题可以很轻松的想到DP的状态转移方程, f [ i ][ j ]=max{ f [ k ][ l ]- ap*(j-l), f [ k ][ r ]+bp*(r-j) } ==> i-k>=w+1, j-l<=as , r-j>=bs

但是稍微估算下复杂度,不加任何优化的话,复杂度为O(T^2*MaxP^2),肯定超时,这时我们考虑一下,每次都是在1~k (i-k>=w+1)这几天里选择最值,那么我们就可以在每次转移是都保存前一天的更优值,转移方程就变成 f [ i ][ j ]=max{ f [ i-w-1 ][ l ]- ap*(j-l), f [ i-w-1 ][ r ]+bp*(r-j) } ==> j-l<=as , r-j>=bs

这样的话,时间复杂度为O(T*maxP^2),还是超时

我们知道,每次都是在第 i-w-1 这天里面找相应的最优值,设v[ j ]=f[ i-w-1 ] - ap* ( maxP-j ),那么我们如果在 i 这天买入物品,使得总物品数为 j 的最优值等于 (j-as)~j 里面 的最大值v[ l ]+ap*(maxP-j) ,这样只需要用单调队列维护这个最优值就可以把复杂度控制在 O(T*maxP),卖出物品的话,同理

PS:这题wa了无数次啊,都是忘记保存前一天的较优值,还有初始化忘记改回来= =

代码:

#include<cstdio>
#include<iostream>
using namespace std;
const int mm=2012;
int f[mm][mm],q[mm],v[mm];
int main()
{
    int i,j,k,n,m,w,t,ap,bp,as,bs,l,r,tmp;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&w);
        for(i=1;i<=w+1;++i)
        {
            scanf("%d%d%d%d",&ap,&bp,&as,&bs);
            for(j=0;j<=m;++j)
            {
                f[i][j]=(j<=as)?-ap*j:-1e9;
                if(i>1)f[i][j]=max(f[i][j],f[i-1][j]);
            }
        }
        for(i=w+2;i<=n;++i)
        {
            scanf("%d%d%d%d",&ap,&bp,&as,&bs);
            k=i-w-1;
            l=0,r=-1;
            for(j=0;j<=m;++j)
            {
                tmp=f[k][j]-ap*(m-j);
                while(l<=r&&v[r]<tmp)--r;
                v[++r]=tmp;
                q[r]=j;
                while(j-q[l]>as)++l;
                f[i][j]=max(f[i-1][j],v[l]+ap*(m-j));
            }
            l=0,r=-1;
            for(j=m;j>=0;--j)
            {
                tmp=f[k][j]+bp*j;
                while(l<=r&&v[r]<tmp)--r;
                v[++r]=tmp;
                q[r]=j;
                while(q[l]-j>bs)++l;
                f[i][j]=max(f[i][j],v[l]-bp*j);
            }
        }
        printf("%d\n",f
[0]);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: