【DP】【单调队列】Trade

传送门

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
Author

lxhgww

Source

HDOJ Monthly Contest – 2010.05.01

Recommend

lcy | We have carefully selected several similar problems for you: 3400 3403 3404 3402 3415


题目大意

知道未来 T T T天的股票买入价格和卖出价格,在满足以下条件下求所能获得的最大收益。

  1. 每天的股票买价 A P i APi APi,卖价 B P i BPi BPi,买和卖都有限制,只能买 A S i ASi ASi支,卖 B S i BSi BSi
  2. 每做一次操作(买或卖)都要隔 W W W天才能做下一次操作
  3. 手上的股票不可以超过 M a x P MaxP MaxP

解题思路

在此推荐某不知名巨佬的博客

DP
f [ i ] [ j ] f[i][j] f[i][j]表示到第 i i i天拥有 j j j支股票
最终答案 m a x ( f [ i ] [ j ] ) max(f[i][j]) max(f[i][j])(0<=i<=n,0<=j<=MaxP)

购买

f[i][j]=max(f[i][j],f[i-w-1][k]-(j-k)*api[i]
       =max(f[i][j],f[i-w-1][k]+k*api[i]-j*api[i]

卖出

f[i][j]=max(f[i][j],f[i-w-1][k]+(k-j)*bpi[i]
       =max(f[i][j],f[i-w-1][k]+k*bpi[i]-j*bpi[i]

可以看出 f [ i − W − 1 ] [ k ] + k ∗ X p [ i ] f[i-W-1][k]+k*Xp[i] f[iW1][k]+kXp[i]是跟k相关的值, j ∗ X p [ i ] j*Xp[i] jXp[i]是与j相关的值
j j j可以直接枚举,而关于 k k k的式子则可以用单调队列优化


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int INF=0x7fffffff;
struct DT{
	int v,num;
}que[2010*2];
int head,tail,T,t,maxp,w,ans;
int api[2010],bpi[2010],asi[2010],bsi[2010],f[2010][2010];
void buy(int i,int j){//买入的状态转移
	while(head<tail&&que[tail-1].v<f[i-w-1][j]+j*api[i])tail--;	
	que[tail].v=f[i-w-1][j]+j*api[i],que[tail++].num=j;
	while((head==-1)||(head<tail&&j-que[head].num>asi[i]))head++;//某不知名巨佬说会有h==-1的情况
	if(que[head].v-j*api[i]>f[i][j])
    	f[i][j]=que[head].v-j*api[i];
}
void sell(int i,int j){//卖出的状态转移
	while(head<tail&&que[tail-1].v<f[i-w-1][j]+j*bpi[i])tail--;	
	que[tail].v=f[i-w-1][j]+j*bpi[i],que[tail++].num=j;
	while((head==-1)||(head<tail&&que[head].num-j>bsi[i]))head++;
	if(que[head].v-j*bpi[i]>f[i][j])
    	f[i][j]=que[head].v-j*bpi[i];
}
int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&t,&maxp,&w);
		for(int i=1;i<=t;i++)
		    scanf("%d%d%d%d",&api[i],&bpi[i],&asi[i],&bsi[i]);
		memset(f,0xcf,sizeof(f));
		for(int i=0;i<=t;i++)f[i][0]=0;//不拥有股票,价值为0
		for(int i=1;i<=w+1;i++)//前1~w+1天都可以买,不能卖
		    for(int j=1;j<=asi[i];j++)
		        f[i][j]=-j*api[i];
		for(int i=2;i<=w+1;i++)//前面有买,但是现在不买不卖
			for(int j=0;j<=maxp;j++)
			    if(f[i-1][j]>f[i][j])
			       f[i][j]=f[i-1][j];
		for(int i=w+2;i<=t;i++){
			for(int j=0;j<=maxp;j++)//前面有买卖,但是现在不买不卖
			    if(f[i-1][j]>f[i][j])
			       f[i][j]=f[i-1][j];
			head=tail=0;
			for(int j=0;j<=maxp;j++)//保证价值可能创造多的在前(买的越多价值越小)
			    buy(i,j);
			head=tail=0;
			for(int j=maxp;j>=0;j--)//保证价值可能创造多的在前(卖的越多价值越大)
			    sell(i,j);
		}
		ans=-INF;
		for(int i=0;i<=t;i++)
		    for(int j=0;j<=maxp;j++)
		        if(f[i][j]>ans)
		           ans=f[i][j];
		printf("%d\n",ans);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单调队列优化DP是一种常用的优化方法,可以将时间复杂度从 $O(n^2)$ 降低到 $O(n)$ 或者 $O(n \log n)$。以下是一道利用单调队列优化DP的典型题目: 题目描述: 给定一个长度为 $n$ 的序列 $a_i$,定义 $f(i)$ 为 $a_i$ 到 $a_n$ 中的最小值,即 $f(i) = \min\limits_{j=i}^n a_j$。现在定义 $g(i)$ 为满足 $f(j) \ge a_i$ 的最小下标 $j$,即 $g(i) = \min\{j \mid j > i, f(j) \ge a_i\}$。如果不存在这样的下标 $j$,则 $g(i) = n+1$。 现在请你计算出 $1 \le i \le n$ 的所有 $g(i)$ 的值。 输入格式: 第一行包含一个整数 $n$。 第二行包含 $n$ 个整数 $a_1,a_2,\cdots,a_n$。 输出格式: 输出 $n$ 行,第 $i$ 行输出 $g(i)$ 的值。 输入样例: 5 3 1 2 4 5 输出样例: 2 5 5 5 6 解题思路: 设 $dp(i)$ 表示 $g(i)$,那么 $dp(i)$ 与 $dp(i+1)$ 的转移关系可以表示为: $$dp(i)=\begin{cases}i+1, &\text{if}\ f(i+1)\ge a_i \\dp(i+1), &\text{else}\end{cases}$$ 这个转移方程可以使用暴力 DP 解决,时间复杂度为 $O(n^2)$。但是,我们可以使用单调队列优化 DP,将时间复杂度降为 $O(n)$。 我们定义一个单调队列 $q$,存储下标。队列 $q$ 中的元素满足: - 队列中的元素是单调递减的,即 $q_1 < q_2 < \cdots < q_k$; - 对于任意的 $i\in [1,k]$,有 $f(q_i) \ge f(q_{i+1})$。 队列 $q$ 的作用是维护一个长度为 $k$ 的区间 $[i+1,q_k]$,满足这个区间中的所有 $j$ 都满足 $f(j) < f(i+1)$。 根据定义,当我们要求 $dp(i)$ 时,只需要查找队列 $q$ 中第一个满足 $f(q_j) \ge a_i$ 的位置 $q_j$,那么 $g(i) = q_j$,如果队列 $q$ 中不存在这样的位置,则 $g(i) = n+1$。 那么如何维护单调队列 $q$ 呢?我们可以在每次 DP 的过程中,将 $i$ 加入队尾。然后判断队首元素 $q_1$ 是否满足 $f(q_1) \ge a_i$,如果满足则弹出队首元素,直到队首元素不满足条件为止。 由于每个元素最多被加入队列一次,并且最多被弹出一次,因此时间复杂度为 $O(n)$。具体实现细节可以参考下面的代码实现:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值