CodeForces 1304F1 Animal Observation (easy version) DP

一、内容

The only difference between easy and hard versions is the constraint on kGildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The color of one camera is red, and the other one's color is blue.
Gildong is going to take videos for ndays, starting from day 1 to day n. The forest can be divided into m areas, numbered from 1 to m. He'll use the cameras in the following way:On every odd day (1-st, 3-rd, 5-th, ...), bring the red camera to the forest and record a video for 2days.On every even day (2-nd, 4-th, 6-th, ...), bring the blue camera to the forest and record a video for 2
days.If he starts recording on the n-th day with one of the cameras, the camera records for only one day.Each camera can observe kconsecutive areas of the forest. For example, if m=5 and k=3, he can put a camera to observe one of these three ranges ofareas for two days: [1,3], [2,4], and [3,5]Gildong got information about how many animals will be seen in each area each day. Since he would like to observe as many animals as possible, he wants you to find the best way to place the two cameras for ndays. Note that if the two cameras are observing the same area on the same day, the animals observed in that area are counted only once.

Input

The first line contains three integers n, m, and k (1≤n≤50, 1≤m≤2⋅104, 1≤k≤min(m,20)) – the number of days Gildong is going to record, the number of areas of the forest, and the range of the cameras, respectively.Next nlines contain m integers each. The j-th integer in the i+1-st line is the number of animals that can be seen on the i-th day in the j-th area. Each number of animals is between 0 and 1000 , inclusive.

Output

Print one integer – the maximum number of animals that can be observed.

Examples

Input

4 5 2
0 2 1 1 0
0 0 3 1 2
1 0 4 3 1
3 3 0 0 4

Output

25

二、思路

在这里插入图片描述

三、代码

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 55, M = 2e4 + 5;
int n, m, k, sum[N][M], lmax[N][M], rmax[N][M], dp[N][M];
int get(int i, int s, int e) {
	return sum[i][e] - sum[i][s - 1];
}
int main() {
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			scanf("%d", &sum[i][j]);
			sum[i][j] += sum[i][j - 1];
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m - k + 1; j++) {
			int ksum = get(i, j, j + k - 1) + get(i + 1, j, j + k - 1);
			if (i == 1) {
				dp[i][j] = ksum; //第一天上面没天数 
				continue;
			}
			for (int x = max(1, j - k + 1); x <= min(j + k - 1, m - k + 1); x++) {
				dp[i][j] = max(dp[i][j], dp[i - 1][x] + ksum - get(i, max(x, j), min(x + k - 1, j + k - 1)));				
			} 	
			//再和没有重叠的部分比较
			if (j > k) dp[i][j] = max(dp[i][j], lmax[i - 1][j - k] + ksum); 
			if (j + k <= m) dp[i][j] = max(dp[i][j], rmax[i - 1][j + k] + ksum); 
		}
		//更新lmax 和 rmax
		for (int j = 1; j <= m - k + 1; j++) {
			lmax[i][j] = max(lmax[i][j - 1], dp[i][j]);
		} 
		for (int j = m - k + 1; j >= 1; j--) {
			rmax[i][j] = max(rmax[i][j + 1], dp[i][j]);
		}
	}
	int ans = 0;
	for (int j = 1; j <= m - k + 1; j++) ans = max(ans, dp[n][j]);
	printf("%d", ans);
	return 0;
} 
发布了456 篇原创文章 · 获赞 466 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104345093
今日推荐