【HDU】3401:Trade【单调队列优化DP】
Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5864 Accepted Submission(s):
2022
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?
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.
5 2 0
2 1 1 1
2 1 1 1
3 2 1 1
4 3 1 1
5 4 1 1
Solution
题目大意是股票在$t$天内每天买或卖或不作为,知道每一天每一支股票的买卖价格$api,bpi$和限购或卖的量$asi,bsi$,以及每天最多持有的股票数$maxp$,还有每次交易必须隔至少$w$的限制,求最大的收益。
DP式定义为$dp[i][j]$表示在第$i$天持有$j$张股票的最大收益。
转移有三方面:
1、不作为:$dp[i][j]=dp[i-1][j]$(所以转移2、3时不用考虑$i-w-1$天前)
2、买进:$dp[i][j]=dp[i-w-1][k]-(j-k)*api=>max(dp[i-w-1][k]+k*api)-j*api$
3、卖出:$dp[i][j]=dp[i-w-1][k]+(k-j)*bpi=>max(dp[i-w-1][k]+k*bpi)-j*bpi$
观察2、3,发现max部分就是单调队列的结构,所以每次对于j找最优决策点(并且满足每天的各种限制),将$n^3$压成了$n^2$。
Code
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std; int t, maxp, w;
int dp[][], ap[], as[], bp[], bs[]; struct Node {
int num, m;
} q[]; int main() {
int T;
scanf("%d", &T);
while(T --) {
scanf("%d%d%d", &t, &maxp, &w);
for(int i = ; i <= t; i ++)
scanf("%d%d%d%d", &ap[i], &bp[i], &as[i], &bs[i]);
for(int i = ; i <= t; i ++)
for(int j = ; j <= maxp; j ++) dp[i][j] = -inf;
for(int i = ; i <= w + ; i ++)
for(int j = ; j <= min(maxp, as[i]); j ++)
dp[i][j] = -j * ap[i];
dp[][] = ;
for(int i = ; i <= t; i ++) {
for(int j = ; j <= maxp; j ++) {
dp[i][j] = max(dp[i][j], dp[i - ][j]);
}
if(i - w - <= ) continue;
int pre = i - w - ;
int h = , t = ;
for(int j = ; j <= maxp; j ++) {
int f = dp[pre][j] + ap[i] * j;
while(h <= t && q[t].m <= f) t --;
q[++ t].num = j;
q[t].m = f;
while(h <= t && q[h].num + as[i] < j) h ++;
dp[i][j] = max(dp[i][j], q[h].m - ap[i] * j);
}
h = ; t = ;
for(int j = maxp; j >= ; j --) {
int f = dp[pre][j] + bp[i] * j;
while(h <= t && q[t].m <= f) t --;
q[++ t].num = j;
q[t].m = f;
while(h <= t && q[h].num - bs[i] > j) h ++;
dp[i][j] = max(dp[i][j], q[h].m - bp[i] * j);
}
}
int res = -inf;
for(int i = ; i <= maxp; i ++) res = max(res, dp[t][i]);
printf("%d\n", res);
}
return ;
}
【HDU】3401:Trade【单调队列优化DP】的更多相关文章
- HDU 3401 Trade(单调队列优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:炒股.第i天买入一股的价钱api,卖出一股的价钱bpi,最多买入asi股,最多卖出bsi股 ...
- HDU 3401 Trade(斜率优化dp)
http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...
- HDU-3401 Trade 单调队列优化DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 DP方程容易想出来,f[i][j]表示第i天拥有j个股票的最优解,则: 1.不买不卖,f[i][ ...
- hdu3401 Trade 单调队列优化dp
Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 【单调队列优化dp】HDU 3401 Trade
http://acm.hdu.edu.cn/showproblem.php?pid=3401 [题意] 知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最 ...
- bzoj1855: [Scoi2010]股票交易 单调队列优化dp ||HDU 3401
这道题就是典型的单调队列优化dp了 很明显状态转移的方式有三种 1.前一天不买不卖: dp[i][j]=max(dp[i-1][j],dp[i][j]) 2.前i-W-1天买进一些股: dp[i][j ...
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- 单调队列优化DP——习题收集
前言 感觉可以用单调队列优化dp的模型还是挺活的,开个随笔记录一些遇到的比较有代表性的模型,断续更新.主要做一个收集整理总结工作. 记录 0x01 POJ - 1821 Fence,比较适合入门的题, ...
- Parade(单调队列优化dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others) ...
- 1855: [Scoi2010]股票交易[单调队列优化DP]
1855: [Scoi2010]股票交易 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1083 Solved: 519[Submit][Status] ...
随机推荐
- oracle环境变量详解
共享存储文件系统(NFS) 通常情况下,ORACLE_SID这个环境变量全称Oracle System Identifier,,用于在一台服务器上标识不同的实例,默认情况下,实例名就是ORACLE_S ...
- vue表格中显示金额格式化与保存时格式化为数字并校验!
最近项目中遇到了成本计算的,需要显示金额,保存一下,以后方便直接拿来用! 一 数字转金额格式显示 //数字转金额格式 format:function(s){ if(/[^0-9\.]/.test(s) ...
- MyBatis 总结记录
1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的XM ...
- Oracle Certified Java Programmer 经典题目分析(一)
Given: 1. public class returnIt { 2. returnType methodA(byte x, double y){ 3. return (short) x/y * 2 ...
- JDOM生成XML文档的一般方法
由于DOM提供的生成XML的方法不够直观,而且要用到各种繁琐的注解,鉴于此可借助第三方库-----JDOM生成XML文档.具体操作方式如下: import java.io.FileOutputStre ...
- Method for balancing binary search trees
Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...
- 解决Win7&Win8 64位下Source Insight提示未完整安装的问题[转]
转自:http://www.cnblogs.com/sixiweb/p/3421533.html 网上的破解版的注册表文件都是针对32位系统的,所以在64位系统里运行根本无法破解.下面分别贴出这俩系统 ...
- Scrapy官网程序执行示例
Windows 10家庭中文版本,Python 3.6.4,Scrapy 1.5.0, Scrapy已经安装很久了,前面也看了不少Scrapy的资料,自己尝试使其抓取微博的数据时,居然连登录页面(首页 ...
- git —— 多人协作(远程库操作)
1.查看远程库信息 $ git remote 2.查看详细远程库信息 $ git remote -v 3.推送分支 $ git push origin 分支名 4.抓取分支 $ git checkou ...
- HDU 3613 Best Reward(manacher求前、后缀回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...