hdu 3401 单调队列优化DP

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 ;
}
上一篇:Android 开源项目DiskLruCache 详解


下一篇:根据当前IP获取当时所在信息