7.20 第一场 Maximal submatrix
Maximal submatrix
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 276 Accepted Submission(s): 76
Problem Description
Given a matrix of n rows and m columns,find the largest area submatrix which is non decreasing on each column
Input
The first line contains an integer T(1≤T≤10)representing the number of test cases.
For each test case, the first line contains two integers n,m(1≤n,m≤2∗103)representing the size of the matrix
the next n line followed. the i-th line contains m integers vij(1≤vij≤5∗103)representing the value of matrix
It is guaranteed that there are no more than 2 testcases with n∗m>10000
Output
For each test case, print a integer representing the Maximal submatrix
Sample Input
1
2 3
1 2 4
2 3 3
Sample Output
4
大概题意
给出一个矩阵,求这个矩阵列上递增的最大子矩阵
思路
遍历矩阵,求每个点所在列递增连续元素个数,然后从底向上遍历矩阵,找到当前行每个元素所在列连续递增元素个数,取最小连续递增元素个数与列数之积即为最大子矩阵。
代码
//
// Created by Black on 2021/7/20.
//
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 3010;
int n, m, t, a[N][N], up[N][N], ans, res;
int main() {
// cin >> t;
scanf("%d", &t);
while (t--) {
memset(up, 0, sizeof up);
// cin >> n >> m;
scanf("%d%d", &n, &m);
// if (n == 1) {
// cout << m << endl;
// continue;
// }
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] >= a[i - 1][j]) {
up[i][j] = up[i - 1][j] + 1;
}
}
}
ans = 0;
for (int i = n; i > 0; i--) {
int last = 0;
res = 10000;
for (int j = 1; j <= m; j++) {
if (up[i][j])
res = min(res, up[i][j] + 1);
else {
ans = max(ans, res * (j - last - 1));
last = j;
}
}
if (res == 10000)
continue;
ans = max(ans, res * (m - last));
}
if (ans < m)
printf("%d\n", m);
else
printf("%d\n", ans);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效