题目链接
【分析】
对于每个格子,预处理出其向左、右能扩展的最远位置。
然后发现这就是悬线法的板子。。。
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 2e3 + 10;
int n, m;
int a[N][N], f[N][N], ans1, ans2;
int U[N][N], L[N][N], R[N][N];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
scanf("%d", &a[i][j]);
L[i][j] = R[i][j] = j;
U[i][j] = 1;
}
for (int i = 1; i <= n; ++i) {
for (int j = 2; j <= m; ++j) {
if (a[i][j] == a[i][j - 1]) continue;
L[i][j] = L[i][j - 1];
}
for (int j = m - 1; j >= 1; --j) {
if (a[i][j] == a[i][j + 1]) continue;
R[i][j] = R[i][j + 1];
}
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
if (i != 1 && a[i][j] != a[i - 1][j]) {
L[i][j] = max(L[i][j], L[i - 1][j]);
R[i][j] = min(R[i][j], R[i - 1][j]);
U[i][j] = U[i - 1][j] + 1;
}
int len2 = R[i][j] - L[i][j] + 1;
int len1 = min(len2, U[i][j]);
ans1 = max(ans1, len1 * len1);
ans2 = max(ans2, len2 * U[i][j]);
}
printf("%d\n%d", ans1, ans2);
return 0;
}