1 5 2 0 2 1 1 1 2 1 1 1 3 2 1 1 4 3 1 1 5 4 1 1
3
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 2010; 4 const int INF = 0x3f3f3f3f; 5 int dp[maxn][maxn],q[maxn],p[maxn],hd,tl; 6 7 int main(){ 8 int kase,ap,bp,as,bs,n,m,w; 9 scanf("%d",&kase); 10 while(kase--){ 11 scanf("%d%d%d",&n,&m,&w); 12 for(int i = 1; i <= w + 1; ++i){ 13 scanf("%d%d%d%d",&ap,&bp,&as,&bs); 14 for(int j = 0; j <= m; ++j){ 15 dp[i][j] = j <= as?-ap*j:-INF; 16 if(i > 1) dp[i][j] = max(dp[i][j],dp[i-1][j]); 17 } 18 } 19 for(int i = w + 2; i <= n; ++i){ 20 scanf("%d%d%d%d",&ap,&bp,&as,&bs); 21 int k = i - w - 1; 22 hd = tl = 0; 23 for(int j = 0; j <= m; ++j){ 24 int tmp = dp[k][j] - ap*(m - j); 25 while(hd < tl && p[tl-1] < tmp) --tl; 26 q[tl] = j; 27 p[tl++] = tmp; 28 while(hd < tl && j - q[hd] > as) ++hd; 29 dp[i][j] = max(dp[i-1][j],p[hd] + ap*(m - j)); 30 } 31 hd = tl = 0; 32 for(int j = m; j >= 0; --j){ 33 int tmp = dp[k][j] + bp*j; 34 while(hd < tl && p[tl-1] < tmp) --tl; 35 q[tl] = j; 36 p[tl++] = tmp; 37 while(q[hd] - j > bs) ++hd; 38 dp[i][j] = max(dp[i][j],p[hd] - bp*j); 39 } 40 } 41 printf("%d\n",dp[n][0]); 42 } 43 return 0; 44 }
原文:http://www.cnblogs.com/crackpotisback/p/4802621.html