标签:front toc 队列 other limit others cstring pre queue
http://acm.hdu.edu.cn/showproblem.php?pid=3401
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5188 Accepted Submission(s): 1776
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 }
标签:front toc 队列 other limit others cstring pre queue
原文地址:http://www.cnblogs.com/zzqc/p/7424383.html