题目链接:

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

Trade

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
#### 问题描述
> 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?

输入

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.

输出

The most money lxhgww can earn.

样例输入

1

5 2 0

2 1 1 1

2 1 1 1

3 2 1 1

4 3 1 1

5 4 1 1

样例输出

3

题意

一个人,一开始有无数的钱和0张股票,接下来的t天里,在第i天,他能够选择以api的价格买进一张股票,且最多允许买asi张;或者以bpi的价格卖出股票,且最多卖出bpi张。任何时刻手头的股票不能超过maxp张,且任意两次交易需要隔至少w天。问最多能赚多少钱。

题解

dp[i][j]表示第i天持有j张股票。

则易知:dp[i][j]=max{dp[i-1][j],dp[i-w-1][k]-(j-k)*api(买入),dp[i-w-1][k]+(k-j)*bpi)(卖出)}

因为当j固定时只需找出max{dp[i-w-1][k]+k*api(或者bpi)},由于有asi(或者bsi)的限制,所以需要用单调队列来维护一下(否则只需要维护一个最大值就ok了)。所以复杂度是O^2。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=2222; int n,maxp,w;
int ap[maxn],bp[maxn],as[maxn],bs[maxn];
///dp[i][j]表示第i天持j张股票
int dp[maxn][maxn];
///单调队列
PII que[maxn]; int main() {
int tc;
scf("%d",&tc);
while(tc--){
scf("%d%d%d",&n,&maxp,&w);
for(int i=0;i<n;i++){
scf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
} ///初始化
for(int i=0;i<n;i++){
for(int j=0;j<=maxp;j++){
dp[i][j]=-INF;
}
}
for(int j=0;j<=min(maxp,as[0]);j++){
dp[0][j]=-ap[0]*j;
} for(int i=1;i<n;i++){
///第i天不进行交易
for(int j=0;j<=maxp;j++){
dp[i][j]=dp[i-1][j];
} ///第i天进行交易
if(i-w-1<0){
for(int j=0;j<=min(maxp,as[i]);j++){
dp[i][j]=max(dp[i][j],-ap[i]*j);
}
continue;
} ///买入
int f=1,r=1;
for(int j=0;j<=maxp;j++){
int x=dp[i-w-1][j]+ap[i]*j;
while(f<r&&que[r-1].X<x) r--;
que[r++]=mkp(x,j);
while(f<r&&que[f].Y+as[i]<j) f++;
dp[i][j]=max(dp[i][j],que[f].X-j*ap[i]);
} ///卖出
f=r=1;
for(int j=maxp;j>=0;j--){
int x=dp[i-w-1][j]+bp[i]*j;
while(f<r&&que[r-1].X<x) r--;
que[r++]=mkp(x,j);
while(f<r&&que[f].Y-bs[i]>j) f++;
dp[i][j]=max(dp[i][j],que[f].X-j*bp[i]);
}
}
int ans=0;
for(int i=0;i<=maxp;i++){
ans=max(ans,dp[n-1][i]);
}
prf("%d\n",ans);
}
return 0;
} //end-----------------------------------------------------------------------

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题目大意:现在要你去炒股,给你每天的开盘价值,每股买入价值为ap,卖出价值为bp,每天最多买as ...

  2. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  3. 1023: [SHOI2008]cactus仙人掌图(DP+单调队列优化)

    这道题吗= =首先解决了我多年以来对仙人掌图的疑问,原来这种高大上的东西原来是这个啊= = 然后,看到这种题,首先必须的就是缩点= = 缩点完之后呢,变成在树上找最长路了= =直接树形dp了 那么那些 ...

  4. Codeforces 1077F2 Pictures with Kittens (hard version)(DP+单调队列优化)

    题目链接:Pictures with Kittens (hard version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:数据量5000, ...

  5. P3084 [USACO13OPEN]照片Photo (dp+单调队列优化)

    题目链接:传送门 题目: 题目描述 Farmer John has decided to assemble a panoramic photo of a lineup of his N cows ( ...

  6. Codeforces 445A Boredom(DP+单调队列优化)

    题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...

  7. bzoj 1855 dp + 单调队列优化

    思路:很容易写出dp方程,很容易看出能用单调队列优化.. #include<bits/stdc++.h> #define LL long long #define fi first #de ...

  8. 股票交易(DP+单调队列优化)

    题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价为每股APi, ...

  9. Luogu 2627 修建草坪 (动态规划Dp + 单调队列优化)

    题意: 已知一个序列 { a [ i ] } ,求取出从中若干不大于 KK 的区间,求这些区间和的最大值. 细节: 没有细节???感觉没有??? 分析: 听说有两种方法!!! 好吧实际上是等价的只是看 ...

随机推荐

  1. WCF 删除队列

    Configuration config = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None); Serv ...

  2. nginx如何设置访问跳转到一个固定页面

    方法一 server {    listen 80 ;    server_name  www.xxx.com xxx.com;      index   test.html  index.html ...

  3. Reducejoin sample

    示例文件同sample join analysis 之前的示例是使用map端的join.这次使用reduce端的join. 根据源的类别写不同的mapper,处理不同的文件,输出的key都是stude ...

  4. ZBrush中如何才能快速完成脸部雕刻(上)

    骨骼,是一门基础艺术,几百年来一直为伟大的艺术大师所研究,它曾经,也将一直是创作现实且可信角色的关键,提高骨骼知识更将大大提高雕刻技能. 查看更多内容请直接前往:http://www.zbrushcn ...

  5. c++实现排序(简单插入,希尔,选择,快速,冒泡,堆排)

    简单插入排序 适用于记录较少且基本有序的记录.算法思想:给定一个存在分界线的序列,分界线左边有序,右边无序,依次将右边的没排序的数与左边序列进行比较,插入相应位置,再对分界线做出相应调整,下面用图来说 ...

  6. ZOJ 3233 Lucky Number --容斥原理

    这题被出题人给活活坑了,题目居然理解错了..哎,不想多说. 题意:给两组数,A组为幸运基数,B组为不幸运的基数,问在[low,high]区间内有多少个数:至少被A组中一个数整除,并且不被B中任意一个数 ...

  7. Codeforces 234D Cinema

    这题做的我好苦啊,编码调试整整搞了一个多小时,而且调到天昏地暗才调出来.. 回归正题,这题是一道本人做过的比较烦,比较无聊的题之一.题意是一个人,在m个影星里有k个喜欢的影星,然后给出n场电影,每场电 ...

  8. javascript中的队列结构

    1.概念 队列和栈结构不同,栈是一种后进先出的结构,而队列是一种先进先出的结构.队列也是一种表结构,不同的是队列只能在队尾插入元素,在队首删除元素,可以将队列想象成一个在超时等待排队付钱的队伍,或者在 ...

  9. iOS宏定义

    1.__OBJC__宏定义作用 在.pch 文件中一般都会自动加上这句宏定义,表示宏内引用的文件确保只被使用Objective-C语言的文件所引用,保证引用关系的清晰.因为在一个OC工程中,可能包含. ...

  10. 在Function对象上扩展method方法

    ;(function() { /** * 在Function对象上扩展method方法 * @param {String} name 扩展的方法名称 * @param {Function} callb ...