传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1177
【题解】
发现分割方案就只有6种……
稍微分点类,然后大力算出以(i,j)为左上角/左下角/右上角/右下角的二维前缀/后缀内的max正方形即可。
然后大力分六种情况讨论一波,注意边界。
【code】
1 # include <stdio.h> 2 # include <string.h> 3 # include <algorithm> 4 // # include <bits/stdc++.h> 5 6 using namespace std; 7 8 typedef long long ll; 9 typedef long double ld; 10 typedef unsigned long long ull; 11 const int M = 1500 + 10; 12 const int mod = 1e9+7; 13 14 # define RG register 15 # define ST static 16 17 int n, m, k, s[M][M]; 18 int p[M][M]; 19 int a[M][M], b[M][M], c[M][M], d[M][M], ans; 20 21 int main() { 22 scanf("%d%d%d", &n, &m, &k); 23 for (int i=1; i<=n; ++i) 24 for (int j=1, x; j<=m; ++j) { 25 scanf("%d", &x); 26 s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + x; 27 } 28 for (int i=k; i<=n; ++i) 29 for (int j=k; j<=m; ++j) 30 p[i][j] = s[i][j] - s[i-k][j] - s[i][j-k] + s[i-k][j-k]; 31 32 // 以(i,j)为右下角 33 for (int i=k; i<=n; ++i) 34 for (int j=k; j<=m; ++j) 35 a[i][j] = max(p[i][j], max(a[i-1][j], a[i][j-1])); 36 37 // 以(i,j)为左下角 38 for (int i=k; i<=n; ++i) 39 for (int j=m-k+1; j; --j) { 40 int C = j+k-1; 41 b[i][j] = max(p[i][C], max(b[i-1][j], b[i][j+1])); 42 } 43 44 // 以(i,j)为右上角 45 for (int i=n-k+1; i; --i) 46 for (int j=k; j<=m; ++j) { 47 int C = i+k-1; 48 c[i][j] = max(p[C][j], max(c[i+1][j], c[i][j-1])); 49 } 50 51 // 以(i,j)为左上角 52 for (int i=n-k+1; i; --i) 53 for (int j=m-k+1; j; --j) { 54 int C1 = i+k-1, C2 = j+k-1; 55 d[i][j] = max(p[C1][C2], max(d[i+1][j], d[i][j+1])); 56 } 57 58 // 两个竖线 59 for (int i=k; i<=n; ++i) 60 for (int j=k+k; j<=m-k; ++j) 61 ans = max(ans, p[i][j] + a[n][j-k] + b[n][j+1]); 62 63 // 两个横线 64 for (int i=k+k; i<=n-k; ++i) 65 for (int j=k; j<=n; ++j) 66 ans = max(ans, p[i][j] + a[i-k][m] + c[i+1][m]); 67 68 // 竖线,横线左 69 for (int i=k; i<=n-k; ++i) 70 for (int j=k; j<=m-k; ++j) 71 ans = max(ans, a[i][j] + c[i+1][j] + d[1][j+1]); 72 73 // 竖线,横线右 74 for (int i=k; i<=n-k; ++i) 75 for (int j=k; j<=m-k; ++j) 76 ans = max(ans, a[n][j] + b[i][j+1] + d[i+1][j+1]); 77 78 // 横线,竖线上 79 for (int i=k; i<=n-k; ++i) 80 for (int j=k; j<=m-k; ++j) 81 ans = max(ans, a[i][j] + b[i][j+1] + d[i+1][1]); 82 83 // 横线,竖线下 84 for (int i=k; i<=n-k; ++i) 85 for (int j=k; j<=m-k; ++j) 86 ans = max(ans, a[i][m] + c[i+1][j] + d[i+1][j+1]); 87 88 printf("%d\n", ans); 89 return 0; 90 }
时间: 2024-10-14 04:25:09