hdu 3401 单调队列优化dp

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?
一个股神,可以预测股市行情的天才.

#include<stdio.h>
#include<algorithm>
using namespace std;
int t,ap,bp,as,bs,dp[2004][2004],q[2004],v[2004],l,r;
int n,maxp,w;
inline const int read()
{
   register int x=0,f=1;
   register char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
   while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
   return f*x;
}
int main()
{
   t=read();
   while(t--)
   {
    n=read(),maxp=read(),w=read();   
    register int i,j;
    for(i=1;i<=w+1;i++)
    {
     ap=read(),bp=read(),as=read(),bs=read();
     for(j=0;j<=maxp;j++)
     {
      if(j<=as) dp[i][j]=-j*ap;
      else dp[i][j]=-210000000;
      if(i>1) dp[i][j]=max(dp[i][j],dp[i-1][j]);
     }
    }
    for(register int i=w+2;i<=n;i++)
    {
     ap=read(),bp=read(),as=read(),bs=read();
     int k=i-w-1;
     l=0,r=-1;
     for(j=0;j<=maxp;j++)
     {
       int tmp=dp[k][j]-ap*(maxp-j);
       while(l<=r&&tmp>v[r]) r--;
       q[++r]=j;
       v[r]=tmp;
       while(j-q[l]>as) l++;
       dp[i][j]=max(dp[i-1][j],v[l]+ap*(maxp-j));
     }
     l=0,r=-1;
     for(j=maxp;j>=0;j--)
     {
       int tmp=dp[k][j]+bp*j;
       while(l<=r&&tmp>v[r]) r--;
       q[++r]=j;
       v[r]=tmp;
       while(q[l]-j>bs) l++;
       dp[i][j]=max(dp[i][j],v[l]-bp*j);
     }
    }
    printf("%d\n",dp[n][0]);
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值