http://acm.hdu.edu.cn/showproblem.php?pid=3401
TradeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5188 Accepted Submission(s): 1776
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 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.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<algorithm> 6 using namespace std; 7 #define inf 120 8 #define LL long long 9 inline int read(int f = 1) 10 { 11 char c = getchar();while (!isdigit(c)) { if (c == '-')f = -1; c = getchar(); } 12 int r = 0; while (isdigit(c)) { r = r * 10 + c - '0';c = getchar(); } return r*f; 13 } 14 int f[2005][2005]; 15 int ap[2005], bp[2005], as[2005], bs[2005]; 16 struct node2 { int w, k; }; 17 deque<node2>q; 18 int main() 19 { 20 int i, j, k, cas, T, MAX_P, W; 21 cin >> cas; 22 while (cas--) { 23 T = read(); 24 MAX_P = read(); 25 W = read(); 26 for (i = 1;i <= T;++i) 27 { 28 ap[i] = read(); 29 bp[i] = read(); 30 as[i] = read(); 31 bs[i] = read(); 32 } 33 memset(f, -inf, sizeof(f)); 34 for (int i = 1; i <= W + 1; i++) {//第一天到W+1天只都是只能买的 35 for (int j = 0; j <= min(MAX_P, as[i]); j++) { 36 f[i][j] = -ap[i] * j; 37 } 38 } 39 f[0][0] = 0; 40 for (i = 1;i <= T;++i) 41 { 42 int u = i - W - 1; 43 for (j = 0;j <= MAX_P;++j) 44 { 45 f[i][j] = max(f[i][j],f[i - 1][j]); 46 if (u < 0)continue; 47 while (!q.empty() && q.back().w < f[u][j] + j*ap[i])q.pop_back(); 48 q.push_back(node2{f[u][j]+j*ap[i],j}); 49 while (!q.empty() && j - q.front().k > as[i])q.pop_front(); 50 if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*ap[i]); 51 } 52 if (u < 0)continue; 53 q.clear(); 54 for (j = MAX_P;j >= 0;--j) 55 { 56 while (!q.empty() && q.back().w < f[u][j] + j*bp[i])q.pop_back(); 57 q.push_back(node2{f[u][j]+j*bp[i],j}); 58 while (!q.empty() && q.front().k -j> bs[i])q.pop_front(); 59 if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*bp[i]); 60 } 61 q.clear(); 62 } 63 int ans = 0; 64 for (i = 0;i <= MAX_P;++i) 65 ans = max(ans,f[T][i]); 66 printf("%d\n", ans); 67 } 68 return 0; 69 }