bzoj1057 [ZJOI2007]棋盘制作

3 3
1 0 1
0 1 0
1 0 0

4
6

HINT

$N, M \leqslant 2000$

Solution

$h[i][j]$ 可以向上延展多长$l[i][j]$$r[i][j]$ 表示 $(i, j)$ 向左/右最多延长到的位置。转移见代码。

#include<bits/stdc++.h>
using namespace std;

int x = 0, flag = 1; char ch = getchar();
while (ch > '9' || ch < '0') { if (ch == '-') flag = -1; ch = getchar(); }
while (ch <= '9' && ch >= '0') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * flag;
}
inline void write(int x) { if (x >= 10) write(x / 10); putchar(x % 10 + '0'); }

#define N 2001
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define drp(i, a, b) for (int i = a; i >= b; i--)
#define ll long long

int n, m;
bool mp[N][N];
int h[N][N], l[N][N], r[N][N];

int main() {
cin >> n >> m;
rep(i, 1, n) rep(j, 1, m) {
if (i == 1) h[i][j] = 1;
else if (mp[i][j] != mp[i - 1][j]) h[i][j] = h[i - 1][j] + 1;
else h[i][j] = 1;
}
int ans1 = 0, ans2 = 0;
rep(i, 1, n) {
rep(j, 1, m) {
l[i][j] = j;
while (l[i][j] > 1 && h[i][l[i][j] - 1] >= h[i][j] &&
mp[i][l[i][j]] != mp[i][l[i][j] - 1])
l[i][j] = l[i][l[i][j] - 1];
}
drp(j, m, 1) {
r[i][j] = j;
while (r[i][j] < m && h[i][r[i][j] + 1] >= h[i][j] &&
mp[i][r[i][j]] != mp[i][r[i][j] + 1])
r[i][j] = r[i][r[i][j] + 1];
}
rep(j, 1, m) {
int t = r[i][j] - l[i][j] + 1;
ans2 = max(ans2, t * h[i][j]), t = min(t, h[i][j]), ans1 = max(ans1, t * t);
}
}
write(ans1), puts(""), write(ans2);
return 0;
}
posted @ 2018-02-05 09:39  aziint  阅读(...)  评论(... 编辑 收藏