Hdu 3401 题解 单调队列优化DP
2017-05-06 11:43
399 查看
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?
且一次交易后至少要间隔w天才能再次交易,初始有0股,本金无限,求最大收益
状态转移 dp[i][j]=max{dp[i-1]j,dp[r][k]-(j-k)*pa[i](i-r>w,j-k<=na[i],买),dp[r][k]+(k-j)*pb[i](i-r>w,k-j<=nb[i],卖)}
所以可以将 r 确定为 i-w-1,复杂度变为t*p*p 还是很大
然后对于买,移项有
dp[i][j]+j*pa[i]=dp[r][k]+k*pa[i]。右边与 j无关, 可见我们只需要对每一个 i 维护一个关于k的单调队列,就可以在 p时间内求出所有的dp[i][j]
1到w+1的都是从初始条件下转移的.
难写
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?
题目大意:
炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股且一次交易后至少要间隔w天才能再次交易,初始有0股,本金无限,求最大收益
题解:
dp[i][j]表示第 i 天,有 j 股的最大收益状态转移 dp[i][j]=max{dp[i-1]j,dp[r][k]-(j-k)*pa[i](i-r>w,j-k<=na[i],买),dp[r][k]+(k-j)*pb[i](i-r>w,k-j<=nb[i],卖)}
复杂度 为 t*t*p*p
首先我们可以看出 dp[i][j]>=dp[i-1][j](不买不卖转移)所以可以将 r 确定为 i-w-1,复杂度变为t*p*p 还是很大
然后对于买,移项有
dp[i][j]+j*pa[i]=dp[r][k]+k*pa[i]。右边与 j无关, 可见我们只需要对每一个 i 维护一个关于k的单调队列,就可以在 p时间内求出所有的dp[i][j]
复杂度降为 t*p 可以接受了
最后注意下边界条件:1到w+1的都是从初始条件下转移的.
代码如下:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N=2000+10; const int INF=0x3f3f3f3f; int T,t,ma,w,ap ,bp ,as ,bs ,dp ; struct data { int p,val; }que[N*2],now; int main() { scanf("%d",&T); while(T--) { scanf("%d%d%d",&t,&ma,&w); for(int i=1;i<=t;i++) scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]); memset(dp,-INF,sizeof(dp)); for(int i=1;i<=w+1;i++) for(int j=0;j<=as[i];j++) dp[i][j]=-ap[i]*(j); for(int i=2;i<=t;i++) { for(int j=0;j<=ma;j++) dp[i][j]=max(dp[i][j],dp[i-1][j]); if(i<=w+1) continue; int front=0,rear=0; for(int j=0;j<=ma;j++) { now.p=j; now.val=dp[i-w-1][j]+ap[i]*j; while(front<rear&&que[rear-1].val<now.val) { rear--; } que[rear++]=now; while((front==-1)||(front<rear&&que[front].p+as[i]<j)) front++; dp[i][j]=max(dp[i][j],que[front].val-ap[i]*j); } front=0,rear=0; for(int j=ma;j>=0;j--) { now.p=j; now.val=dp[i-w-1][j]+bp[i]*j; while(front<rear&&que[rear-1].val<now.val) { rear--; } que[rear++]=now; while((front==-1)||(front<rear&&que[front].p-bs[i]>j)) front++; dp[i][j]=max(dp[i][j],que[front].val-bp[i]*j); } } int ans=0; for(int i=0;i<=ma;i++) ans=max(ans, dp[t][i]); printf("%d\n",ans); } return 0; }
难写
相关文章推荐
- hdu 3401 单调队列优化DP
- HDU 3401 Trade (单调队列优化DP)
- hdu 3401 单调队列优化+dp
- hdu 3401 Trade 单调队列优化dp
- hdu 3401 单调队列优化dp
- HDU 3401 Trade 【DP+单调队列优化】
- bzoj1855: [Scoi2010]股票交易 单调队列优化dp ||HDU 3401
- hdu 3401 单调队列优化dp
- HDU 3401 单调队列优化DP
- hdu 3401(单调队列优化dp)
- hdu 3401 Trade(DP+单调队列优化)
- hdu 3401 单调队列优化DP
- hdu 3401 Trade 单调队列优化dp
- hdu 3401 Trade(单调队列优化dp)
- HDU 3401 Trade 单调队列优化DP
- 股票交易 【SCOI 2010】HDU 3401 (单调队列优化DP入门)
- HDU 3401 Trade(单调队列优化DP)【模板】
- HDU-3401 Trade 单调队列优化DP
- hdu 3401 Trade(单调队列优化DP)
- hdu-3401-Trade-单调队列优化的DP