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. Spring4笔记11--SSH整合2--SpringWeb

    SSH 框架整合技术: 2. Spring 在 Web 项目中的使用(建立在Spring与Hibernate整合的基础上): 在 Web 项目中使用 Spring 框架,首先要解决在 Servlet ...

  2. Android Framebuffer介绍及使用【转】

    转自:https://www.jianshu.com/p/df1213e5a0ed 来自: Android技术特工队 作者: Aaron 主页: http://www.wxtlife.com/ 原文连 ...

  3. bzoj 1034 泡泡堂BNB

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1034 题解: 很明显的贪心,读过田忌赛马的典故就很容易能想出来,分成三种情况讨论: < ...

  4. Pytorch数据读取框架

    训练一个模型需要有一个数据库,一个网络,一个优化函数.数据读取是训练的第一步,以下是pytorch数据输入框架. 1)实例化一个数据库 假设我们已经定义了一个FaceLandmarksDataset数 ...

  5. MongoDB官方文档结构

    本文展示MongoDB 3.6.4.0的官方Server文档的结构图——一眼可见完整的知识脉络图.不过,MongoDB除了Server的文档外,还有DRIVERS.CLOUD.TOOLS.DUIDES ...

  6. 牛客红包OI赛 C 小可爱表白

    据说是个公式题. Code #include<cstdio> #include<cstring> #include<algorithm> using namespa ...

  7. java小爬虫

    爬取煎蛋网 1.找出页面网址的规律 2.设计页面图片网址的正则 代码: import java.io.BufferedInputStream; import java.io.BufferedOutpu ...

  8. **iOS发JSON请求中字符串加转义,返回的JSON去转义

    iOS中使用NSSerialization把对象转为JSON字符串后,多出来反斜杠的问题 http://segmentfault.com/q/1010000000576646 NSDictionary ...

  9. 在MVC中如何使用vs2013编译调试razor cshtml

    打开mvc项目的csproj文件: <MvcBuildViews>false</MvcBuildViews> 改为 <MvcBuildViews>true</ ...

  10. android拾遗——Android之Service与IntentService的比较

    不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentService,前段时间准备面试的时候看到了一篇关于IntentServic ...