Trade

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

Appoint description: 

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
 
题意:
总共有T天,任何时刻最多只能有MaxP股票,如果第i天买股票,那么必须在第i + W + 1天或之后 在买,卖也是如此。
第i天能够买AS[i]的股票,能够卖BS[i]的股票,买的价格为AP[i],卖的价格为BP[i],开始的时候没有任何股票,但是有足够的
钱。问能最多赚多少钱。
思路:
dp[i][j]表示第i天有j个股票能赚多少钱。
那么可以分三种情况:
1)什么都不做 dp[i][j] = max(dp[i][j],dp[i-1][j]);
2)买了股票 dp[i][j] = max(dp[i][j],dp[i-W-1][k] - (j - k) * AP[i]);
3)卖了股票 dp[i][j] = max(dp[i][j],dp[i-W-1][k] + (k - j) * BP[i]); 
 
按照朴素的做法枚举天数,股票数j,k复杂度为O(n ^ 3);
将买股票的状态方程转化一下成 dp[i][j] = max(dp[i-W-1][k] + k * AP[i]) - j * AP[i];令f[i-W-1] = dp[i-W-1][k] + k * AP[i],
那么dp[i][j] = max(f[i-W-1]) - j * AP[i];对于f[i-W-1]可以用单调队列来求。这样复杂度就成了n ^ 2。买股票的也类似。
 
/*
* Author: sweat123
* Created Time: 2016/7/12 13:52:38
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int num;
int val;
node(int tnum = ,int tval = ):num(tnum),val(tval){}
};
deque<node>q;
int dp[MAXN][MAXN],T,MaxP,W;
int AP[MAXN],BP[MAXN],AS[MAXN],BS[MAXN];
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++){//第一天到W+1天只都是只能买的
for(int j = ; j <= min(MaxP,AS[i]); j++){
dp[i][j] = - AP[i] * j;
}
}
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;//已经预处理了
q.clear();
int bf = i - W - ;//队列中保存i - W - 1天的信息
for(int j = ; j <= MaxP; j++){//有多少股票
int nowf = dp[bf][j] + j * AP[i];//队列维护这个最大值
while(!q.empty() && q.back().val < nowf){
q.pop_back();
}
q.push_back(node(j,nowf));
while(!q.empty() && j - q.front().num > AS[i]){//队列里面存的都是i - W - 1天的信息 对于第i天
//如果第i天的股票数 - (i - W - 1)天的股票数大于今天能买的数,那就不符合
q.pop_front();
}
dp[i][j] = max(dp[i][j],q.front().val - j * AP[i]);
}
q.clear();
for(int j = MaxP; j >= ; j--){
int nowf = dp[bf][j] + j * BP[i];
while(!q.empty() && q.back().val < nowf){
q.pop_back();
}
q.push_back(node(j,nowf));
while(!q.empty() && q.front().num - j > BS[i]){
q.pop_front();
}
dp[i][j] = max(dp[i][j],q.front().val - j * BP[i]);
}
}
int ans = ;
for(int i = ; i <= MaxP; i++){
ans = max(ans,dp[T][i]);
}
printf("%d\n",ans);
}
return ;
}

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

  1. hdu 3401 单调队列优化+dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  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. iOS---设置输入框的光标位置

    //这里设置光标位置,让光标位置后移10 textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)]; tex ...

  2. 【代码笔记】iOS-获得徐家汇的天气预报

    一,代码. //获得徐家汇的天气预报 -(void)getWeatherInfo{ NSError *error; NSURLRequest *request = [NSURLRequest requ ...

  3. IOS开发之功能模块--输入框随着键盘的位置移动而移动

    废话不多说,先直接上效果图: 先熟悉一下在Cocoa框架中会用到的key键: 然后直接上Demo的源码截图: 看代码之前,补充说一句,Demo中的文本框以及文本框的背后灰色的View是通过storyb ...

  4. 在 ASP.NET MVC 中充分利用 WebGrid (microsoft 官方示例)

    在 ASP.NET MVC 中充分利用 WebGrid https://msdn.microsoft.com/zh-cn/magazine/hh288075.aspx Stuart Leeks 下载代 ...

  5. Mindjet MindManager思维导图工具的使用 - 项目管理系列文章

    在项目管理过程中,不可避免的要使用到各种各样的项目管理工具.本文就对我自己使用的Mindjet Manager进行一下介绍,推荐给大家使用. 1.  打开软件 这里使用了2012版 2.  软件的界面 ...

  6. SQL SERVER 临时表导致存储过程重编译(recompile)的一些探讨

    SQLSERVER为了确保返回正确的值,或者处于性能上的顾虑,有意不重用缓存在内存里的执行计划,而重新编译执行计划的这种行为,被称为重编译(recompile).那么引发存储过程重编译的条件有哪一些呢 ...

  7. 如何让Log4net日志文件按每月归成一个文件夹,StaticLogFileName参数的用法

    想要让Log4net日志(以下称日志)按每月自动归类为一个文件夹,为此,学习和修改了log4net.config文件.查了资料,重点是以下这些参数:      <param name=" ...

  8. GL.IssuePluginEvent 发布插件事件

    Description 描述 Send a user-defined event to a native code plugin. 发送一个用户定义的事件到一个本地代码插件. Rendering in ...

  9. 13、Apache中虚拟目录和目录权限配置

    一.虚拟目录 之前的个人主页,为了安全起见,需要把~yanji 用户隐藏起来,这时就可以设置个 虚拟目录. 它在Apache服务器应用比较多,能够隐藏系统的真实目录,实用性非常高. 虚拟目录主要 通过 ...

  10. [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ

    前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的 ...