HDU 3401 Trade 【DP+单调队列优化】
来源:互联网 发布:中医古籍数据库公司 编辑:程序博客网 时间:2024/05/22 17:46
题意
给出接下来T天每天卖出、买入股票的价格,每天买入、卖出的上限,持有的股票的总上限,并且两次股票操作之间有时间间隔,求T天之后最多能赚多少钱。
分析
很容易可以写出状态转移方程:
其中
按照转移方程,朴素的做法必然是
AC代码
//POJ 3401 Trade//AC 2017-01-16 12:18:10//DP, Monotonic queue#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cctype>#include <cstdlib>#include <cstring>#include <vector>#include <set>#include <string>#include <map>#include <queue>#include <deque>#include <list>#include <sstream>#include <stack>using namespace std;#define cls(x) memset(x,0,sizeof x)#define inf(x) memset(x,0x3f,sizeof x)#define neg(x) memset(x,-1,sizeof x)#define ninf(x) memset(x,0xc0,sizeof x)#define st0(x) memset(x,false,sizeof x)#define st1(x) memset(x,true,sizeof x)#define lowbit(x) x&(-x)#define input(x) scanf("%d",&(x))#define inputt(x,y) scanf("%d %d",&(x),&(y))#define bug cout<<"here"<<endl;//#pragma comment(linker, "/STACK:1024000000,1024000000")//stack expansion//#define debugconst double PI=acos(-1.0);const int INF=0x3f3f3f3f;//1061109567-2147483647const long long LINF=0x3f3f3f3f3f3f3f3f;//4557430888798830399-9223372036854775807const int maxn=2000+100;int t;int n,MaxP,W;int AP[maxn],BP[maxn],AS[maxn],BS[maxn];int dp[maxn][maxn];int monqueA[maxn],monqueB[maxn],sa,ea,sb,eb;void insertA(int pre,int i,int x){ while(sa<ea&&dp[pre][monqueA[ea-1]]+AP[i]*monqueA[ea-1]<=dp[pre][x]+AP[i]*x) --ea; monqueA[ea++]=x;}void insertB(int pre,int i,int x){ while(sb<eb&&dp[pre][monqueB[eb-1]]+BP[i]*monqueB[eb-1]<=dp[pre][x]+BP[i]*x) --eb; monqueB[eb++]=x;}int main(){ //ios::sync_with_stdio(false); //cin.tie(0); #ifdef debug freopen("E:\\Documents\\code\\input.txt","r",stdin); freopen("E:\\Documents\\code\\output.txt","w",stdout); #endif //IO scanf("%d",&t); while(t--) { scanf("%d %d %d",&n,&MaxP,&W); for(int i=1;i<=n;++i) scanf("%d %d %d %d",AP+i,BP+i,AS+i,BS+i); ninf(dp); dp[0][0]=0; for(int i=1;i<=n;++i) { sa=ea=0; sb=eb=0; int pre=max(0,i-W-1); for(int j=0;j<=min(MaxP,BS[i]-1);++j) insertB(pre,i,j); for(int j=0;j<=MaxP;++j) { dp[i][j]=dp[i-1][j]; insertA(pre,i,j); if(j+BS[i]<=MaxP) insertB(pre,i,j+BS[i]); while(sa<ea&&monqueA[sa]<j-AS[i]) ++sa; while(sb<eb&&monqueB[sb]<j) ++sb; if(sa<ea) dp[i][j]=max(dp[i][j],dp[pre][monqueA[sa]]+AP[i]*monqueA[sa]-AP[i]*j); if(sb<eb) dp[i][j]=max(dp[i][j],dp[pre][monqueB[sb]]+BP[i]*monqueB[sb]-BP[i]*j); } } int res=0; for(int i=0;i<=MaxP;++i) res=max(res,dp[n][i]); printf("%d\n",res); } return 0;}
0 0
- hdu 3401 Trade(DP+单调队列优化)
- hdu 3401 Trade 单调队列优化dp
- hdu 3401 Trade 单调队列优化dp
- hdu 3401 Trade(单调队列优化dp)
- HDU 3401 Trade 单调队列优化DP
- HDU 3401 Trade 【DP+单调队列优化】
- HDU 3401 Trade (单调队列优化DP)
- hdu-3401-Trade-单调队列优化的DP
- HDU-3401:Trade(dp+单调队列优化)
- HDU 3401 Trade(单调队列优化DP)【模板】
- HDU 3401 Trade(用单调队列优化DP)
- Trade HDU 3401 单调队列DP
- hdu 3401 Trade //单调队列+DP
- HDU 3401 Trade【单调队列+dp】
- hdu 3401 Trade 单调队列+dp
- hdu 3401 Trade dp+单调队列
- hdu 3401 Trade 【单调队列+dp】
- hdu 3401 Trade (单调队列优化)
- Jquery表单插件ajaxForm
- 针对web阶段http相关知识小结
- 判断CPU大小端模式
- 关于看算法导论不懂的时候的思考
- TCP、UDP、IP 协议分析
- HDU 3401 Trade 【DP+单调队列优化】
- 当你在试衣间试衣服,请你务必想起wait()与notify()
- 算法学习---关于快速排序,数据呈现基本有序和完全无序时候导致的时间复杂度增n^2的解决方案
- spark mllib和ml类里面的区别
- MySQL数据库与网页进行数据传递(上)
- first-child first-of-type last-child last-of-type 伪类选择器总结
- 【数据结构】-线性表-链表 熟练度max=2
- MySQL之——重连,连接丢失:The last packet successfully received from the serve
- js addEventListener和attachEvent函数中的this的指向问题及解决方法