hdu 3401 單調佇列+DP
http://acm.hdu.edu.cn/showproblem.php?pid=3401
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?
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.
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
/**
hdu 3401 單調佇列+DP
http://blog.csdn.net/acm_ted/article/details/7909742
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 2005
#define inf 0xfffff
int T,MaxP,w;
int APi[MAX],BPi[MAX],ASi[MAX],BSi[MAX];
int dp[MAX][MAX];
struct node
{
int x;
int p;
}q[2005],temp;
int front,back;
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
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);
for(int i=0;i<=T;i++)
for(int j=0;j<=MaxP;++j)
dp[i][j]=-inf;
for(int i=1;i<=w+1;i++)
for(int j=0;j<=ASi[i];++j)
dp[i][j]=(-APi[i]*j);
for(int i=2;i<=T;i++)
{
for(int j=0;j<=MaxP;j++)
dp[i][j]=max(dp[i][j],dp[i-1][j]);
if(i<=w+1) continue;
//買入
front=back=1;
for(int j=0;j<=MaxP;j++)
{
temp.p=j;
temp.x=dp[i-w-1][j]+APi[i]*j;
while(front<back&&q[back-1].x<temp.x)
back--;
q[back++]=temp;
while(front<back&&q[front].p+ASi[i]<j)
front++;
dp[i][j]=max(dp[i][j],q[front].x-APi[i]*j);
}
front=back=1;
for(int j=MaxP;j>=0;j--)
{
temp.p=j;
temp.x=dp[i-w-1][j]+BPi[i]*j;
while(front<back&&q[back-1].x<temp.x)
back--;
q[back++]=temp;
while(front<back&&q[front].p-BSi[i]>j)
front++;
dp[i][j]=max(dp[i][j],q[front].x-BPi[i]*j);
}
}
int ans=0;
for(int i=0;i<=MaxP;i++)
ans=max(ans,dp[T][i]);
printf("%d\n",ans);
}
return 0;
}
相關文章
- hdu4374單調佇列+dp佇列
- HDU 3530 Subsequence (dp+單調佇列)佇列
- hdu 3415 單調佇列佇列
- HDU 3530 單調佇列佇列
- hdu 4122 單調佇列佇列
- POJ 3017 單調佇列dp佇列
- HDU3415 Max Sum of Max-K-sub-sequence (DP+單調佇列)佇列
- poj3017 dp+單調佇列佇列
- HDU3530 單調佇列的應用佇列
- 單調佇列佇列
- 單調佇列雙端佇列佇列
- hdu3415 單調佇列求區間最大和佇列
- hdu 4122Alice's mooncake shop(單調佇列)佇列
- BZOJ1044: [HAOI2008]木棍分割(dp 單調佇列)佇列
- 聯賽模擬測試18 A. 施工 單調佇列(棧)優化DP佇列優化
- POJ 2823 單調佇列佇列
- HDU 4123 Bob's Race:樹的直徑 + 單調佇列 + st表佇列
- BZOJ 2442 [Usaco2011 Open]修剪草坪:單調佇列優化dp佇列優化
- 佇列-單端佇列佇列
- 單調佇列優化dp(五)——#10179. 「一本通 5.5 例 5」Banknotes佇列優化
- hdu5040 優先佇列+bfs佇列
- noi.ac #289. 電梯(單調佇列)佇列
- bzoj1597: [Usaco2008 Mar]土地購買(斜率優化+Dp+單調佇列)優化佇列
- HDU 5884-Sort(佇列+二分)佇列
- hdu5380 貪心+雙端佇列佇列
- HDU4427Math Magic (dp+滾動陣列)陣列
- 洛谷P3195 [HNOI2008]玩具裝箱TOY(單調佇列優化DP)佇列優化
- 多重揹包問題的單調佇列優化佇列優化
- C語言 簡單的佇列(陣列佇列)C語言佇列陣列
- HDU 1227 Fast Food(簡單二維dp)AST
- hdu 1069 Monkey and Banana(簡單dp)NaN
- 【BFS+優先佇列】HDU 3442 Three Kingdoms佇列
- HDU4546 比賽難度 (優先佇列)佇列
- hdu 4546 優先佇列 數列組合和第m小佇列
- POJ 2823Sliding Window(單調佇列水題)佇列
- HDU 3853 LOOPS(概率dp)OOP
- HDU 4427 Math Magic【dp+優化+滾動陣列】【好題】優化陣列
- HDU5630 Hiking(貪心+優先佇列)佇列