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. speech sdk 文字转语音

    1.下载SDK包 https://www.microsoft.com/en-us/download/details.aspx?id=10121 2.直接上代码 // SpeechRecognition ...

  2. MySQL权限系统(一).The MySQL Access Privilege System 概述

    纯属个人阅读,如有翻译错误,请指出 The primary function of the MySQL privilege system is to authenticate a user who c ...

  3. 如何在Pycharm设置ES6语法环境

    首先 如果不进行相关设置就刚ES6 语法的话,会出现下面提示性错误(运行还是能正常出效果的): (let 飘红, 这只是其中之一, 其他语法也会飘红) 接着,就是解决问题: 首先打开设置: 接着找到下 ...

  4. MySQL 第四天

    回顾 列属性: 主键, 自增长, 唯一键     关系: 一对一,一对多和多对多 范式: 三层范式 1NF: 字段设计必须符合原子性 2NF: 不存在部分依赖(没有复合主键) 3NF: 不存在传递依赖 ...

  5. Linux服务器维护常用命令

    # uname -a # 查看内核/操作系统/CPU信息 # /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostname # 查看计算 ...

  6. 剑指offer 面试57题

    面试57题: 题目:和为s的数字 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的.   输出描述: 对应每个测 ...

  7. 函数编程——匿名函数与lambda(一)

    python允许用lambda关键字创造匿名函数. 匿名函数是因为不需要以标准的方式来声明,比如说,使用def语句. 但是,作为函数,它们也能有参数. 一个完整的lambda“语句”代表了一个表达式, ...

  8. 流量分析系统---echarts模拟迁移中 ,geocoord从后台获取动态数值

    由于在echarts的使用手册中说了 {Object} geoCoord (geoCoord是Object类型) ,所以不能用传统的字符串拼接或数组的方式赋值.在后台的controller中用Map& ...

  9. Loadrunder脚本篇——webservice接口测试(一)

    函数介绍 soap_request 函数执行一个SOAP请求 函数原型 int soap_request( const char *StepName, ExpectedResponse, URL, , ...

  10. VC引用动态库

    1. 程序所在目录 2. 进程当前目录 3. 系统目录(C:\WINDOWS\System32) 4. Windows目录(C:\WINDOWS) 5. PATH环境变量指向的目录