POJ 3494 Largest Submatrix of All 1’s
来源:互联网 发布:大数据概念龙头股 编辑:程序博客网 时间:2024/05/19 20:40
Description
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? Bylargest we mean that the submatrix has the most elements.
The input contains multiple test cases. Each test case begins with m andn (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order onm lines each with n numbers. The input ends once EOF is met.
For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.
2 20 00 04 40 0 0 00 1 1 00 1 1 00 0 0 0
04
题目大意:
给定一个M x N的01矩阵, 找出最大的全1子矩阵。
解题思路:
数据量是2000, 如果枚举每一个位置能拓展的矩阵大小会超时, 所以我们需要逐行枚举, 用一个栈去维护一个递增的K x 1的矩阵, 当遇到一个下降的矩阵时就向前拓展看它最长能拓展的大小, 输出那个最大的答案即可(思路与POJ 2559相同)。
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
/*
*ios::sync_with_stdio(false);
*/
*ios::sync_with_stdio(false);
*/
typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = 2010;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = 2010;
int n, m;
int arr[Max][Max], h[Max];
int arr[Max][Max], h[Max];
int main() {
// initialization
while (~scanf("%d %d", &n, &m)) {
memset(h, 0 ,sizeof(h));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
scanf("%d", &arr[i][j]);
}
}
// initialization
while (~scanf("%d %d", &n, &m)) {
memset(h, 0 ,sizeof(h));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
scanf("%d", &arr[i][j]);
}
}
int ans = 0, l, w;
for (int i = 1; i <= n; ++i) {
// i-th 1 numbers
for (int j = 1; j <= m; ++j) {
h[j] = (arr[i][j] == 0) ? 0 : h[j] + 1;
}
for (int i = 1; i <= n; ++i) {
// i-th 1 numbers
for (int j = 1; j <= m; ++j) {
h[j] = (arr[i][j] == 0) ? 0 : h[j] + 1;
}
// prevent overfull
stack < int> S; S.push(0); h[++m] = 0;
for (int j = 1; j <= m; ++j) {
while (h[j] < h[S.top()]) {
l = h[S.top()]; S.pop();
w = j - S.top() - 1;
ans = max(ans, l * w);
}
S.push(j);
}
--m;
}
printf("%d\n", ans);
}
return 0;
}
stack < int> S; S.push(0); h[++m] = 0;
for (int j = 1; j <= m; ++j) {
while (h[j] < h[S.top()]) {
l = h[S.top()]; S.pop();
w = j - S.top() - 1;
ans = max(ans, l * w);
}
S.push(j);
}
--m;
}
printf("%d\n", ans);
}
return 0;
}
阅读全文
0 0
- Largest Submatrix of All 1’s POJ
- Largest Submatrix of All 1’s POJ
- poj 3494 Largest Submatrix of All 1’s
- POJ 3494 Largest Submatrix of All 1’s
- poj 3494 Largest Submatrix of All 1’s
- POJ-3494-Largest Submatrix of All 1’s
- poj 3494 Largest Submatrix of All 1’s
- Largest Submatrix of All 1’s(POJ 3494) 单调栈
- POJ 3494 Largest Submatrix of All 1’s 已翻译
- poj 3494 Largest Submatrix of All 1’s(单调栈)
- poj 3494 Largest Submatrix of All 1’s
- poj 3494 Largest Submatrix of All 1’s
- POJ 3494 Largest Submatrix of All 1’s
- poj3494 Largest Submatrix of All 1’s
- Largest Submatrix of All 1’s--POJ3494
- Largest Submatrix of All 1’s
- Largest Submatrix of All 1’s
- Largest Submatrix of All 1’s
- 【linux小白学习之路】ubuntu安装有道词典
- 创建spring mvc 项目
- 编程的学法
- 设计模式(4)-适配器(Adapter)模式
- SDL 2.0 API by Category
- POJ 3494 Largest Submatrix of All 1’s
- Visual Studio+OpenCV 出现错误“应用程序无法正常启动(0x000007b)”的解决方法
- LeetCode 160: Intersection of Two Linked Lists
- 阻塞队列
- 安装配置Maven(二)
- git 用法总结
- $ajax中的confirm、complete、setup用法归纳
- Nginx配置文件nginx.conf中文详解
- MySql ERROR 1075 (42000) at line 1:...