首页 > 其他分享 >ARC081F

ARC081F

时间:2022-11-11 13:44:12浏览次数:33  
标签:int ARC081F mx Up stk ans top

首先能发现一个矩阵是合法的,当且仅当它的每一行都和第一行一样,或正好相反。

充分性很显然,但是必要性我还不会()

令 \(s\) 表示原矩阵,\(a_{i,j}=[s_{i,j}=\#]\)。

构造数组 \(b_{i,j}=a_{i,j}\oplus a_{i+1,j}\)。

则可以发现一个合法矩阵,每一行一定是全 \(0\) 或全 \(1\)。

于是可以在 \(\mathcal O(n^2)\) 时间内递推处理出每个位置最多能往左扩展的长度 \(mx_{i,j}\),用单调栈处理出每个位置向上最多能扩展到的位置 \(Up_{i,j}\),向下最多能扩展到的位置 \(Down_{i,j}\)。

然后就可以用 \((Down_{i,j}-Up_{i,j}+2)\times mx_{i,j}\) 更新答案。

注意单独的行和列也是合法的,于是答案要和 \(n,m\) 取个 \(\max\)。

Code:

#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, m, ans;
char s[N][N];
int a[N][N];
int mx[N], Up[N], Down[N];
int stk[N], top;

int main() {
	scanf("%d%d", &n, &m), ans = max(n, m);
	for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
	for (int i = 1; i < n; ++i) {
		for (int j = 1; j <= m; ++j)
			a[i][j] = s[i][j] != s[i + 1][j];
		a[i][0] = -1;
	}
	for (int j = 1; j <= m; ++j) {
		for (int i = 1; i < n; ++i)
			if (a[i][j] == a[i][j - 1]) ++mx[i];
			else mx[i] = 1;
		top = 0;
		for (int i = 1; i < n; ++i) {
			while (top && mx[stk[top]] >= mx[i]) --top;
			Up[i] = stk[top] + 1, stk[++top] = i;
		}
		top = 0;
		for (int i = n - 1; i; --i) {
			while (top && mx[stk[top]] >= mx[i]) --top;
			Down[i] = top ? stk[top] - 1 : n - 1, stk[++top] = i;
		}
		for (int i = 1; i < n; ++i)
			ans = max(ans, (Down[i] - Up[i] + 2) * mx[i]);
	}
	printf("%d", ans);
	return 0;
}

标签:int,ARC081F,mx,Up,stk,ans,top
From: https://www.cnblogs.com/Kobe303/p/16880243.html

相关文章

  • [ARC081F] Flip and Rectangles
    考虑转换题目给出的条件。可以观察到一些性质若某个矩形能被操作为全\(1\),那么其任意子矩形也一定可以。任意行列交换不影响矩阵是否能变为全\(1\)然后重要的来了......