hdu 3401 单调队列优化dp

来源:互联网 发布:mac翻墙代理软件 编辑:程序博客网 时间:2024/05/24 05:03

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]);   }}
原创粉丝点击