hdu3401 Trade 单调队列优化dp
Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2918 Accepted Submission(s): 930
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?
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.
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
Recommend
lcy
这里错了好多次了,首先,我们可以得出dp[i][j]=fmax(dp[i][k]+ap[ii]*(k-j),dp[i][k]+bp[ii]*(k-j)) dp[i][j]表求第i天有j只,这样,复杂度太高了,我们可以发现,我们就可以通过dp[i][j]的先一天得到最大值,这样我们就可以减成n^3了,我们可以把dp[i][k]+bp[i]*k 和dp[i][k]+ap[i][k],用一个单调队列存起来,这样可以优化成了n^2,也就可以过了,但是要注意,这里,一定要把小于m的开始单独初始化,这里易错!
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; #define MAXN 2005 #define inf -1000000000 int ap[MAXN],bp[MAXN],as[MAXN],bs[MAXN],dp[MAXN][MAXN],queuea[MAXN],queueb[MAXN],maxp; int fmax(int a,int b) { if(a>b) return a; return b; } int ffa(int i,int k,int ii) { return dp[i][k]+k*bp[ii]; } int ffb(int i,int k,int ii) { return dp[i][k]-(maxp-k)*ap[ii]; } int main() { int tcase,i,ii,j,k,n,m,s,e,l,r; scanf("%d",&tcase); while(tcase--) { scanf("%d%d%d",&n,&maxp,&m); for(i=1;i<=n;i++) { scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]); } for(i=1;i<=m+1;i++) for(j=0;j<=maxp;j++) { dp[i][j]=(j<=as[i])?-ap[i]*j:inf; if(i>1)dp[i][j]=fmax(dp[i][j],dp[i-1][j]); } for(ii=m+2;ii<=n;ii++) { i=ii-m-1; l=0;r=-1; for(j=0;j<=maxp;j++) { while(l<=r&&ffb(i,j,ii)>ffb(i,queueb[r],ii)) { r--; } queueb[++r]=j; while(((j-queueb[l]>as[ii]))) { l++; } k=queueb[l]; dp[ii][j]=fmax(dp[ii-1][j],dp[i][k]+ap[ii]*(k-j)); } s=0;e=-1; ; for(j=maxp;j>=0;j--) { while(s<=e&&ffa(i,j,ii)>ffa(i,queuea[e],ii)) { e--; } queuea[++e]=j; while((queuea[s]-j>bs[ii])) { s++; } k=queuea[s]; dp[ii][j]=fmax(dp[ii][j],dp[i][k]+bp[ii]*(k-j)); } } int maxx=dp[n][0]; printf("%d\n",dp[n][0]); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)