HDU3401(dp + 单调队列优化)
来源:互联网 发布:jdk 7u67 linux x64 编辑:程序博客网 时间:2024/05/22 11:40
Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4945 Accepted Submission(s): 1689
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?
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.
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
题意:一个人买股票,有T天,每天买可以最多买As[i]只股票,每只花费A[i],每天最多卖Bs[i]只股票,每只可以卖B[i],在任何时刻手里的股票数目最多不能超过MaxP只,并且必须间隔天数大于等于W+ 1才能交易.问你这个人最多赚多少钱。
解题思路:我们用dp[i][j],表示前i天,第i天之后刚好剩下j只股票的最多能赚到的钱。显然我们可以从dp[i - w - 1][k] 进行转移,但是这个dp不够优,是n*n*n的复杂度,所以我们可以用单调队列进行优化,我们每次维护一个max{dp[i - w - 1][k] + k*A[i]}或max(dp[i -w - 1][k] + k*B[i]},具体看你是进行买还是卖。
#include<bits/stdc++.h>using namespace std;const int maxn = 2e3 + 10;const int inf = 1e9;int T, MaxP, W;int dp[maxn][maxn];int A[maxn], B[maxn], AS[maxn], BS[maxn];int tail, head;struct node{ int id; int value;};node q[maxn];int main(){ int t; scanf("%d", &t); while(t--) { for(int i = 0; i < maxn; i++) { for(int j = 0; j < maxn; j++) { dp[i][j] = -inf; } } //dp[0][0] = 0; scanf("%d%d%d", &T, &MaxP, &W); for(int i = 1; i <= T; i++) { scanf("%d%d%d%d", &A[i], &B[i], &AS[i], &BS[i]); } for(int i = 1; i <= W + 1; i++) { for(int j = 0; j <= AS[i] && j <= MaxP; j++) { dp[i][j] = -j * A[i]; } } for(int i = 1; i <= T; i++) { for(int j = 0; j <= MaxP; j++) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); } if(i <= W + 1) continue; int pre = i - W - 1; //买入 head = 0; tail = 1; node nx; for(int j = 0; j <= MaxP; j++) { int ans = dp[pre][j] + j * A[i]; while(head >= tail && q[head].value < ans) head--; nx.id = j; nx.value = ans; q[++head] = nx; while(head >= tail && j - q[tail].id > AS[i]) tail++; dp[i][j] = max(dp[i][j], q[tail].value - j * A[i]); } //卖出 head = 0; tail = 1; for(int j = MaxP; j >= 0; j--) { int ans = dp[pre][j] + j * B[i]; while(head >= tail && q[head].value < ans) head--; nx.id = j; nx.value = ans; q[++head] = nx; while(head >= tail && q[tail].id - j > BS[i]) tail++; dp[i][j] = max(dp[i][j], q[tail].value - j * B[i]); } } int Max = 0; for(int i = 0; i <= MaxP; i++) { Max = max(Max, dp[T][i]); } printf("%d\n", Max); } return 0;}
- hdu3401 Trade(单调队列优化dp)
- HDU3401(dp + 单调队列优化)
- HDU3401 单调队列优化DP
- Hdu3401 Trade(dp 单调队列优化)最详细题解
- Trade-----HDU3401----单调队列优化的DP
- hdu3401 Trade 单调队列优化dp
- [HDU3401]Trade && 单调队列优化DP
- hdu3401 Trade 单调队列优化DP
- hdu3401 Trade [单调队列优化dp]
- 单调队列优化dp [HDU2191][HDU3401][POJ1821]
- 【HDU3401】Trade-单调队列优化DP
- [hdu3401] Trade DP单调队列优化
- 【DP+单调队列】 hdu3401 Trade
- [单调队列DP] HDU3401 Trade
- 单调队列 例子:hdu3401 trade(dp加单调队列)
- 单调队列 hdu3401 Trade
- 单调队列优化DP
- 单调队列优化DP
- 第六次上机实验
- CollapsingToolbarLayout使用介绍
- Java 中 String 之 compareTo()的程序源码浅析
- 51Nod 1244 莫比乌斯函数前缀和
- Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f解析
- HDU3401(dp + 单调队列优化)
- 【Effective C++】读书笔记 条款49~51
- ubuntu屏幕亮度调节
- logistic regression 分类:机器学习
- ORACLE创建用户并授权
- 排列序号
- python 整数越界问题
- 详解js中Number()、parseInt()和parseFloat()的区别
- 1125: 杨辉三角