http://acm.hdu.edu.cn/showproblem.php?pid=3401

Trade

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

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
  感觉初始化有点坑,,,,
由于间隔w天才能进行第二次购买,对于第i天想进行交易的股票来说之前的w天如果可以转移说明之前没进行过交易,所以这几天的数值都一样,所以不必重复的更新,
只从前i-w-1这一天转移就好了。
so  f[i][j]=MAX{f[i][j-1], f[i-w-1][k]-(j-k)*ap[i] k<=j&&j-k<=as[i], f[i-w-1][k]+(k-j)*bp[i] | k>=j&&k-j<=bs[i] };
显然可以用单调队列加速,但是注意要提前处理前w天所有购买后的花费, 因为对于i-w-1小于零的天我们直接忽略,导致并没有计算他们,所以切记初始化!
不必考虑前w+1天卖,因为如果想卖说明前面买了,但是两天都处于前w+1天中间隔肯定小于w天,因此证明得前w+1天里不可能发生卖的操作!
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 120
#define LL long long
inline int read(int f = )
{
char c = getchar();while (!isdigit(c)) { if (c == '-')f = -; c = getchar(); }
int r = ; while (isdigit(c)) { r = r * + c - '';c = getchar(); } return r*f;
}
int f[][];
int ap[], bp[], as[], bs[];
struct node2 { int w, k; };
deque<node2>q;
int main()
{
int i, j, k, cas, T, MAX_P, W;
cin >> cas;
while (cas--) {
T = read();
MAX_P = read();
W = read();
for (i = ;i <= T;++i)
{
ap[i] = read();
bp[i] = read();
as[i] = read();
bs[i] = read();
}
memset(f, -inf, sizeof(f));
for (int i = ; i <= W + ; i++) {//第一天到W+1天只都是只能买的
for (int j = ; j <= min(MAX_P, as[i]); j++) {
f[i][j] = -ap[i] * j;
}
}
f[][] = ;
for (i = ;i <= T;++i)
{
int u = i - W - ;
for (j = ;j <= MAX_P;++j)
{
f[i][j] = max(f[i][j],f[i - ][j]);
if (u < )continue;
while (!q.empty() && q.back().w < f[u][j] + j*ap[i])q.pop_back();
q.push_back(node2{f[u][j]+j*ap[i],j});
while (!q.empty() && j - q.front().k > as[i])q.pop_front();
if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*ap[i]);
}
if (u < )continue;
q.clear();
for (j = MAX_P;j >= ;--j)
{
while (!q.empty() && q.back().w < f[u][j] + j*bp[i])q.pop_back();
q.push_back(node2{f[u][j]+j*bp[i],j});
while (!q.empty() && q.front().k -j> bs[i])q.pop_front();
if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*bp[i]);
}
q.clear();
}
int ans = ;
for (i = ;i <= MAX_P;++i)
ans = max(ans,f[T][i]);
printf("%d\n", ans);
}
return ;
}

hdu 3401 单调队列优化+dp的更多相关文章

  1. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  2. hdu 3401 单调队列优化动态规划

    思路: 动态方程很容易想到dp[i][j]=max(dp[i][j],dp[i-w-1][j-k]-k*ap[i],dp[i-w-1][j+k]+k*bp[i]): dp[i][j]表示第i天拥有j个 ...

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

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

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

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

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

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

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

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

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

  8. bzoj1855: [Scoi2010]股票交易--单调队列优化DP

    单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...

  9. hdu3401:单调队列优化dp

    第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...

随机推荐

  1. 前端基础-html(2)

    一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong>.<em>.<sup>.<sub&g ...

  2. 单例 与 static

    单例的构造器是private的,不能直接用new 创建对象.static虽然可以随时使用,但是还是有被重新创建的可能. 举个例子,你希望任何时候有一个class A的实例就可以了class B {  ...

  3. Linux中的服务管理

    RPM包默认安装的服务 查看已安装的服务: chkconfig --list 默认安装位置: /etc/init.d 启动脚本 /etc/sysconfig 初始化环境配置文件 /etc  配置文件位 ...

  4. (4.4)dbcc checkdb 数据页修复

    转自:http://blog.51cto.com/lzf328/955852 三篇 一.创建错误数据库 以前看Pual写过很多数据恢复的文章,他很多的测试都是自己创建的Corrupt数据库,其实我们自 ...

  5. (扫盲)WebSocket 教程

    原文地址:http://www.ruanyifeng.com/blog/2017/05/websocket.html WebSocket 是一种网络通信协议,很多高级功能都需要它. 本文介绍 WebS ...

  6. python基础23 -----进程和线程

    一.进程 1.什么是进程? 1.1 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成. 1.2 程序是指进程需要完成那些功能以及如何完成. 1.3 数据集 ...

  7. 【转】Python的hasattr() getattr() setattr() 函数使用方法详解

    Python的hasattr() getattr() setattr() 函数使用方法详解 hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值 ...

  8. vscode使用vue中的v-for提示错误

    "vetur.validation.template": false 在设置里面把vetur.validation.template改为false 文件→首选项→设置 搜索vetu ...

  9. 主攻ASP.NET MVC4.0之重生:MVC Controller修改Controller.tt模版,自动添加版本注释信息

    第一步找到MVC 4.0 CodeTemplates 一般路径在:C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Ite ...

  10. 外网IP地址API

    新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...