1427 - Parade (dp+单调队列)

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 west-east roads and m + 1 north-south roads. Of course, there are (n + 1)×(m + 1) 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 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 minutes. If it takes p minutes to pass a love-hate zone, we say the length of that love-hate zone is p . 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.

Input 

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


Each test case consists of n + 3 lines.

The first line contains three integers: n , m and k . (0 < n100, 0 < m10000, 0k3000000)

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

The last n + 1 lines also stands for n + 1 west-east roads in north to south order. Each line contains mintegers showing the lengths (in minutes) of the road's m 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

题意:F城由n+1个横向路和m+1个竖向路组成。横向路每条路有时间和快乐值,现在每段横向路走的时间不能超过k,问从南到北能走的最大快乐值。

思路:dp,需要单调队列去优化,状态不难想,dp[i][j]表示走到(i,j)这个位置的最大快乐值,那么dp[i][j] = dp[i - 1][k] + sum[j] - sum[k](if时间满足),可是这样的话复杂度是o(n*m^2),需要用单调队列去优化,设func(x) = sum[x] - dp[i - 1][x];如此一来dp[i][j] = sum[j] - func(k); 用单调队列去维护func(k)的最小值即可。复杂度优化为o(n*m)。注意要从左往右和从右往左各一次,然后记录下较大的。

代码:

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define INF 0x3f3f3f3f
const int N = 105;
const int M = 10005;
int n, m, k, i, j, Happy[N][M], Time[N][M], dp[N][M][3], sumh[N][M][2], sumt[N][M][2];
deque<int> Q;

int func(int i, int j, int bo) {
	return sumh[i][j][bo] - dp[i - 1][j][2];
}
int solve() {
	int ans = -INF;
	for (i = 1; i <= n + 1; i++) {
		Q.clear();
		for (j = 0; j <= m; j++) {
			dp[i][j][0] = -INF;
			while (!Q.empty() && sumt[i][j][0] - sumt[i][Q.front()][0] > k) Q.pop_front();
			while (!Q.empty() && func(i, j, 0) <= func(i, Q.back(), 0)) Q.pop_back();
			Q.push_back(j);
			dp[i][j][0] = max(dp[i][j][0], sumh[i][j][0] - func(i, Q.front(), 0));
		}
		Q.clear();
		for (j = m; j >= 0; j--) {
			dp[i][j][1] = -INF;
			while (!Q.empty() && sumt[i][j][1] - sumt[i][Q.front()][1] > k) Q.pop_front();
			while (!Q.empty() && func(i, j, 1) <= func(i, Q.back(), 1)) Q.pop_back();
			Q.push_back(j);
			dp[i][j][1] = max(dp[i][j][1], sumh[i][j][1] - func(i, Q.front(), 1));
		}
		for (j = 0; j <= m; j++) {
			dp[i][j][2] = max(dp[i][j][0], dp[i][j][1]);
			if (i == n + 1)
				ans = max(ans, dp[i][j][2]);
		}
	}
	return ans;
}

int main() {
	while (~scanf("%d%d%d", &n, &m, &k) && n + m + k) {
		for (i = 1; i <= n + 1; i++) {
			for (j = 1; j <= m; j++) {
				scanf("%d", &Happy[i][j]);
				sumh[i][j][0] = sumh[i][j - 1][0] + Happy[i][j];
			}
			sumh[i][m + 1][1] = Happy[i][m + 1] = 0;
			for (j = m; j >= 0; j--) {
				sumh[i][j][1] = sumh[i][j + 1][1] + Happy[i][j + 1];
			}
		}
		for (i = 1; i <= n + 1; i++) {
			for (j = 1; j <= m; j++) {
				scanf("%d", &Time[i][j]);
				sumt[i][j][0] = sumt[i][j - 1][0] + Time[i][j];
			}
			sumt[i][m + 1][1] = Time[i][m + 1] = 0;
			for (j = m; j >= 0; j--) {
				sumt[i][j][1] = sumt[i][j + 1][1] + Time[i][j + 1];
			}
		}
		printf("%d\n", solve());
	}
	return 0;
}


你可能感兴趣的:(1427 - Parade (dp+单调队列))