【BZOJ1177】[Apio2009]Oil【前缀和】【分类讨论】

【题目链接】

一开始想搞三个单调队列跑,似乎会T,没思路,看了发题解,吓傻。

【trinkle的题解】

读入优化会RE,于是贡献了一发RE...

/* Pigonometry */
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 1505;

int n, m, k, s[maxn][maxn], a[maxn][maxn], b[maxn][maxn], c[maxn][maxn], d[maxn][maxn];

inline int iread() {
	int x; scanf("%d", &x);
	return x;
}

int main() {
	n = iread(); m = iread(); k = iread();

	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + iread();
	for(int i = n; i >= k; i--) for(int j = m; j >= k; j--) s[i][j] -= s[i - k][j] + s[i][j - k] - s[i - k][j - k];

	for(int i = k; i <= n; i++) for(int j = k; j <= m; j++) a[i][j] = max(s[i][j], max(a[i - 1][j], a[i][j - 1]));
	for(int i = k; i <= n; i++) for(int j = m; j >= k; j--) b[i][j] = max(s[i][j], max(b[i - 1][j], b[i][j + 1]));
	for(int i = n; i >= k; i--) for(int j = m; j >= k; j--) c[i][j] = max(s[i][j], max(c[i + 1][j], c[i][j + 1]));
	for(int i = n; i >= k; i--) for(int j = k; j <= m; j++) d[i][j] = max(s[i][j], max(d[i + 1][j], d[i][j - 1]));

	int ans = 0;
	for(int i = k; i <= n - k; i++) for(int j = k; j <= m - k; j++) ans = max(ans, a[i][j] + b[i][j + k] + d[i + k][m]);
	for(int i = n; i >= k + k; i--) for(int j = m; j >= k + k; j--) ans = max(ans, c[i][j] + d[i][j - k] + a[i - k][m]);
	for(int i = k; i <= n - k; i++) for(int j = k; j <= m - k; j++) ans = max(ans, a[i][j] + d[i + k][j] + b[n][j + k]);
	for(int i = n; i >= k + k; i--) for(int j = m; j >= k + k; j--) ans = max(ans, c[i][j] + b[i - k][j] + a[n][j - k]);
	for(int i = k + k; i <= n - k; i++) for(int j = k; j <= m; j++) ans = max(ans, s[i][j] + a[i - k][m] + d[i + k][m]);
	for(int i = k; i <= n; i++) for(int j = k + k; j <= m - k; j++) ans = max(ans, s[i][j] + a[n][j - k] + b[n][j + k]);

	printf("%d\n", ans);
	return 0;
}


你可能感兴趣的:(【BZOJ1177】[Apio2009]Oil【前缀和】【分类讨论】)