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. Redis作者谈Redis应用场景(转)

    add by zhj : 这是Redis的作者antirez在他的技术博客中写的一篇文章 英文原文:take-advantage-of-redis-adding-it-to-your-stack 译文 ...

  2. CentOS 7.4 下设置定时任务

    cron介绍 我们经常使用的是crontab命令是cron table的简写,它是cron的配置文件,也可以叫它作业列表,我们可以在以下文件夹内找到相关配置文件. /var/spool/cron/ 目 ...

  3. 0x07 MySQL 多表查询

    Some Content From——Egon's Blog http://www.cnblogs.com/linhaifeng/articles/7126847.html 一 准备表 准备表 #建表 ...

  4. 内置函数: filter 和 map

    内置函数———filter和map filter filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表.接收两个参数,第一个为函数,第二个为序列,序列的每个元素作 ...

  5. 我的Android进阶之旅------>Android使用百度地图时,关于android.permission.BAIDU_LOCATION_SERVICE的声明警告。

    [重要提醒] 定位SDKv3.1版本之后,以下权限已不需要,请取消声明,否则将由于Android 5.0多帐户系统加强权限管理而导致应用安装失败. <uses-permission androi ...

  6. 给俺的 CSDN 博客加背景音乐 - 高大尚的《心经》背景音乐

    给俺的 CSDN 博客加背景音乐 - 高大尚的<心经>背景音乐 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途- ...

  7. win10笔记本触摸板手势大全

  8. 印象笔记windows端-快捷键大全

    作为印象笔记粉,当然要多掌握些快捷键,提高办公效率. 补: ctrl + shift + , 光标内字体变小 ctrl + shitf + . 光标内字体变大

  9. Redis集群环境搭建

    Redis集群cluster环境搭建 描述:本章节主要单服务器搭建集群,在一个服务器上启动多个不同端口的redis服务,非真实环境. 真实环境下redis集群会搭建在多个物理服务器上,并非单一的服务器 ...

  10. 树莓派从 DHT11 温度湿度传感器读取数据

    时序图参考厂家说明书:DHT11数字湿温度传感器的原理和应用范例 四个阵脚连接:VCC接3.3伏电源,Dout接GPIO口,我接的是物理12针脚,NC留空,GND接地. 波折1:电阻被错接进了VCC, ...