Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5864    Accepted Submission(s):
2022

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
 
Author
lxhgww
 
Source
 
Recommend
lcy

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】的更多相关文章

  1. HDU 3401 Trade(单调队列优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:炒股.第i天买入一股的价钱api,卖出一股的价钱bpi,最多买入asi股,最多卖出bsi股 ...

  2. HDU 3401 Trade(斜率优化dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...

  3. HDU-3401 Trade 单调队列优化DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 DP方程容易想出来,f[i][j]表示第i天拥有j个股票的最优解,则: 1.不买不卖,f[i][ ...

  4. hdu3401 Trade 单调队列优化dp

    Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  5. 【单调队列优化dp】HDU 3401 Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 [题意] 知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最 ...

  6. 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 ...

  7. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  8. 单调队列优化DP——习题收集

    前言 感觉可以用单调队列优化dp的模型还是挺活的,开个随笔记录一些遇到的比较有代表性的模型,断续更新.主要做一个收集整理总结工作. 记录 0x01 POJ - 1821 Fence,比较适合入门的题, ...

  9. Parade(单调队列优化dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others)    ...

  10. 1855: [Scoi2010]股票交易[单调队列优化DP]

    1855: [Scoi2010]股票交易 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1083  Solved: 519[Submit][Status] ...

随机推荐

  1. Count 1 in Binary

    Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return 1 Given 5, r ...

  2. Shell-help格式详解

    前言 linux shell命令通常可以通过-h或--help来打印帮助说明,或者通过man命令来查看帮助,有时候我们也会给自己的程序写简单的帮助说明,其实帮助说明格式是有规律可循的 帮助示例 下面是 ...

  3. Java代码优化总结

    代码优化是一个很重要的课题.一般来说,代码优化的目标主要有两个,一个是减小代码的体积,另一个是提高代码运行的效率. 代码优化的细节有很多,此处列举部分: 1.尽量指定类.方法的final修饰符. 带有 ...

  4. dragstart drag dragend dragenter dragover dragleave drop

    dragstart drag dragend dragenter dragover dragleave drop   前端框架层出不穷,网页上的效果越来越绚丽,制作绚丽的效果的成本越来越低,其中有种拖 ...

  5. wpf listBox的item,滚动条拖到低部,底部内容不能完全显示的问题

    listBox外部包裹一层 <ScrollViewer VerticalScrollBarVisibility="Auto"> 然后修改listBox的style,取消 ...

  6. Spring cloud Feign 调用端不生效

    如果提供方的接口经过测试是没问题的话. 消费方启动类加上@EnableFeignClients 注意定义的接口如果不和启动类在同一个包路径下,需要加basePackages 即:@EnableFeig ...

  7. 在VirtualBox虚拟机中安装Centos操作系统怎么与本地XShell远程连接

    问题: 在VirtualBox安装好了CentOS操作系统后,我们怎么才可以用XSell连接虚拟机中的CentOS呢? 答案: (1)在windows下用cmd--ipconfig查看VirtualB ...

  8. (二)HtmlUnit 使用

    第一节: htmlunit 模拟浏览器请求 第二节: htmlunit 获取指定元素 第三节: htmlunit 使用代理 IP 第四节: htmlunit 取消 css,javascript 支持 ...

  9. sql server中分布式查询随笔(链接服务器(sp_addlinkedserver)和远程登录映射(sp_addlinkedsrvlogin)使用小总结)

    由于业务逻辑的多样性,经常得在sql server中查询不同数据库中数据,这就产生了分布式查询的需求 现我将开发中遇到的几种查询总结如下: 1.access版本 --建立连接服务器 EXEC sp_a ...

  10. GreenPlum学习笔记:create table创建表

    二维表同样是GP中重要的存储数据对象,为了更好的支持数据仓库海量数据的访问,GP的表可以分成: 面向行存储的普通堆积表 面向列存储的AOT表(append only table) 当然AOT表也可以是 ...