Largest Submatrix of All 1’s
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 8551   Accepted: 3089
Case Time Limit: 2000MS

Description

Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

Input

The input contains multiple test cases. Each test case begins with m and n (1 ≤ mn ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with nnumbers. The input ends once EOF is met.

Output

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.

Sample Input

2 2
0 0
0 0
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0

Sample Output

0
4

题意:

求内部全部都是1的子矩阵最大面积。

思路:

第一时间想到了一个n的3次方的dp,但是这样肯定超时。

类似的题 :https://www.cnblogs.com/ZGQblogs/p/10664506.html

如果不是在学单调栈,我觉得我一定是想不到的。

首先就是维护每一行的每一个位置,如果以当前行为底,上面连续的1有多少个。

然后就是这个题:http://poj.org/problem?id=2559

代码:

加了读入挂才过~~~

 #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); int l[maxn],r[maxn];
struct node{
int num,pos;
};
int mp[maxn][maxn];
stack<node>st; int solve(int *num,int n){
for(int i=;i<=n;i++){
l[i]=r[i]=i;
}
num[]=num[n+]=-;
for(int i=;i<=n+;i++){
while(!st.empty()&&st.top().num>num[i]){
r[st.top().pos]=i-;
st.pop();
}
st.push(node{num[i],i});
}
while(!st.empty()){st.pop();} for(int i=n;i>=;i--){
while(!st.empty()&&st.top().num>num[i]){
l[st.top().pos]=i+;
st.pop();
}
st.push(node{num[i],i});
}
ll ans=;
for(int i=;i<=n;i++){
ans=max(ans,1ll*num[i]*(r[i]-l[i]+));
}
while(!st.empty()){st.pop();}
return ans;
} char buf[maxn], *ps = buf, *pe = buf+;
inline void rnext(){
if(++ps == pe)
pe = (ps = buf)+fread(buf,,sizeof(buf),stdin);
}
template <class T>
inline bool in(T &ans)
{
ans = ;
T f = ;
if(ps == pe) return false;
do{
rnext();
if('-' == *ps) f = -;
}while(!isdigit(*ps) && ps != pe);
if(ps == pe) return false;
do
{
ans = (ans<<)+(ans<<)+*ps-;
rnext();
}while(isdigit(*ps) && ps != pe);
ans *= f;
return true;
} int main()
{
// freopen("in.txt","r",stdin);
int n,m;
while(true){
in(n);in(m);
if(!m||!n){break;}
int ans=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
in(mp[i][j]);
// cout<<mp[i][j]<<endl;
if(mp[i][j]==){mp[i][j]+=mp[i-][j];}
}
ans=max(ans,solve(mp[i],m));
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. SQL Server 2008 阻止保存要求重新创建表的更改问题的设置方法
  2. linux系统top命令查看系统状态
  3. InnoDB与Myisam的六大区别
  4. 【python】 [基础] 数据类型,字符串和编码
  5. COM中的REFIID小解【转】
  6. Bootstrap 我的学习记录2 栅格系统初识
  7. redis学习笔记——(1)
  8. hdu 1730 Northcott Game 博弈论
  9. android MIPI屏 导航栏丢失
  10. Character Encoding tomcat.
  11. 蓝牙-b
  12. Swift UI学习UITableView and protocol use
  13. HDU 4585 Shaolin (set的应用)
  14. Oracle中主键、外键、索引、序列、唯一性约束的创建
  15. Mysql高性能笔记(一):Schema与数据类型优化
  16. 工控安全入门之Ethernet/IP
  17. openfire开发文档
  18. win7下使用U盘安装双系统(Ubuntu-17)
  19. HDU 6095 17多校5 Rikka with Competition(思维简单题)
  20. Selenium学习笔记

热门文章

  1. vue-router 页面布局
  2. solr8.0 springboot整合solr(四)
  3. 深圳市共创力推出独家课程《AHB和OSG》高级实务培训课程!
  4. Hive参数
  5. 如何使用java validation api进行参数校验----Hibernate-Validation
  6. Linux环境下将Oracle11g数据库模式由非归档模式(Noarchivelog)修改为自动归档模式(archivelog)
  7. Node.js在指定的图片模板上生成二维码图片并附带底部文字说明
  8. 《重构》的读书笔记&ndash;方法列表
  9. 分布式存储ceph——(6)ceph 讲解
  10. 通过java代码执行Linux命令查询声卡和显卡 型号