首页 > 代码库 > LA 4327

LA 4327

Panagola, The Lord of city F likes to parade very much. He always inspects his city in his car and enjoys the welcome of his citizens. City F has a regular road system. It looks like a matrix with n + 1 <tex2html_verbatim_mark>west-east roads and m + 1 <tex2html_verbatim_mark>north-south roads. Of course, there are (n + 1)×(m + 1) <tex2html_verbatim_mark>road crosses in that system. The parade can start at any cross in the southernmost road and end at any cross in the northernmost road. Panagola will never travel from north to south or pass a cross more than once. Citizens will see Panagola along the sides of every west-east road. People who love Panagola will give him a warm welcome and those who hate him will throw eggs and tomatoes instead. We call a road segment connecting two adjacent crosses in a west-east road a ``love-hate zone". Obviously there are m <tex2html_verbatim_mark>love-hate zones in every west-east road. When passing a love-hate zone, Panagola may get happier or less happy, depending on how many people love him or hate him in that zone. So we can give every love-hate zone a ``welcome value" which may be negative, zero or positive. As his secretary, you must make Panagola as happy as possible. So you have to find out the best route --- of which the sum of the welcome values is maximal. You decide where to start the parade and where to end it.

When seeing his Citizens, Panagola always waves his hands. He may get tired and need a break. So please never make Panagola travel in a same west-east road for more than k <tex2html_verbatim_mark>minutes. If it takes p <tex2html_verbatim_mark>minutes to pass a love-hate zone, we say the length of that love-hate zone is p <tex2html_verbatim_mark>. Of course you know every love-hate zone‘s length.

The figure below illustrates the case in sample input. In this figure, a best route is marked by thicker lines.

 

\epsfbox{p4327.eps}<tex2html_verbatim_mark>

 

Input 

There are multiple test cases. Input ends with a line containing three zeros.

 


Each test case consists of n + 3 <tex2html_verbatim_mark>lines.

The first line contains three integers: n <tex2html_verbatim_mark>, m <tex2html_verbatim_mark>and k <tex2html_verbatim_mark>. (0 < n$ \le$100, 0 < m$ \le$10000, 0$ \le$k$ \le$3000000)<tex2html_verbatim_mark>

The next n + 1 <tex2html_verbatim_mark>lines stands for n + 1 <tex2html_verbatim_mark>west-east roads in north to south order. Each line contains m <tex2html_verbatim_mark>integers showing the welcome values of the road‘s m <tex2html_verbatim_mark>love-hate zones, in west to east order.

The last n + 1 <tex2html_verbatim_mark>lines also stands for n + 1 <tex2html_verbatim_mark>west-east roads in north to south order. Each line contains m<tex2html_verbatim_mark>integers showing the lengths (in minutes) of the road‘s m <tex2html_verbatim_mark>love-hate zones, in west to east order.

 

Output 

For each test case, output the sum of welcome values of the best route. The answer can be fit in a 32 bits integer.

 

Sample Input 

 

2 3 2 7 8 1 4 5 6 1 2 3 1 1 1 1 1 1 1 1 1 0 0 0

 

Sample Output 

 

27

dp模型不难想,单调队列从左到右,从右到左优化一次就可以了
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7  8 using namespace std; 9 10 #define read() freopen("sw.in", "r", stdin)11 12 typedef long long ll;13 const int MAX_N = 105;14 const int MAX_M = 10006;15 const ll INF = 1e10 + 7;16 int N, M, K;17 int wv[MAX_N][MAX_M];18 int ma[2 * MAX_M];19 ll dp[MAX_N][MAX_M];20 ll sumt[MAX_N][MAX_M];21 ll sub[MAX_M];22 23 void solve() {24         memset(dp, 0, sizeof(dp));25 26         for (int i = N + 1; i >= 1; --i) {27                 int s = 0, e = 0;28                 ll now = 0;29                 memset(sub, 0, sizeof(sub));30                 for (int j = 1; j <= M + 1; ++j) {31 32                         if (s < e)33                         dp[i][j] = max(dp[i][j], dp[i + 1][ ma[s] ] + now + sub[ ma[s] ]);34                         dp[i][j] = max(dp[i + 1][j], dp[i][j]);35                         while (s < e && dp[i + 1][j] > dp[i + 1][ ma[e - 1] ] + now + sub[ ma[e - 1] ]) --e;36                         ma[e++] = j;37                         sub[j] = -now;38                         now += wv[i][j];39 40                         while (s < e && sumt[i][j + 1] - sumt[i][ ma[s] ] > K)  {41                                 ++s;42 43                         }44                 }45 46                 s = 0, e = 0, now = 0;47                 memset(sub, 0, sizeof(sub));48                 for (int j = M + 1; j >= 1; --j) {49                         if (s < e)50                         dp[i][j] = max(dp[i][j], dp[i + 1][ ma[s] ] + now + sub[ ma[s] ]);51                         dp[i][j] = max(dp[i + 1][j], dp[i][j]);52                         while (s < e && dp[i + 1][j] > dp[i + 1][ ma[e - 1] ] + now + sub[ ma[e - 1] ]) --e;53                         ma[e++] = j;54                         sub[j] = -now;55                         now += wv[i][j - 1];56 57                         while (s < e && sumt[i][ ma[s] ] - sumt[i][j - 1] > K)  {58                                 ++s;59                         }60                 }61         }62 63         ll ans = -INF;64         for (int i = 1; i <= M + 1; ++i) {65                 ans = max(ans, dp[1][i]);66         }67 68         printf("%lld\n", ans);69 70 }71 72 int main()73 {74     //read();75     while (~scanf("%d%d%d", &N, &M, &K) && (N || M || K)) {76             for (int i = 1; i <= N + 1; ++i) {77                     for (int j = 1; j <= M; ++j) {78                             scanf("%d", &wv[i][j]);79                     }80             }81 82             memset(sumt, 0, sizeof(sumt));83 84             for (int i = 1; i <= N + 1; ++i) {85                     for (int j = 1; j <= M; ++j) {86                             int ch;87                             scanf("%d", &ch);88                             sumt[i][j + 1] += sumt[i][j] + ch;89                     }90             }91 92             solve();93     }94     return 0;95 }
View Code