传送门

Time Limit: 2 Sec  Memory Limit: 128 MB

Description

We
often use the matrix to analyze reality models. There are lots of
algorithm about matrix in Linear Algebra like matrix multiplication,
matrix determinant and matrix inversion, etc.

Recently, I should use matrix to do structural mechanics analysis.
The element in the matrix indicating the mechanical properties of each
unit in the structure. Stable sub-structure means a part with same
mechanical properties. I want to find the largest stable sub-struture as
it has good engineering applications. Reflected in the matrix, the
problem above equals to find the largest sub-matrix whose members have
the same value.

To accomplish the task perfectly, I wish you can help me to design a good algorithm to solve this problem.

Input

There are multiple test cases.

The first line contains two integers N and M, indicating the size of this N * M matrix A.

The next N line, each line containing M integers. The j-th integer in the i-th line means the element A(i, j).

1 <= N, M <= 800
1 <= A(i, j) <= 1000

Output

For each test, output the size of the largest sub-matrix satisfied the requests.

Sample Input

3 3
1 1 1
1 2 1
1 1 1
 
 
2 2
1 2
3 4

 
4 4
1 1 1 2
1 3 3 2
5 3 3 2
6 6 6 7

Sample Output

3
1
4

HINT

Source

2013大连市赛


Solution

单调栈


Implementation

#include <cstdio>
#include <stack>
using namespace std;
typedef long long LL; const int N(+); int h[N], L[N], R[N], a[N][N]; stack<int> st;
//[L[i], R[i])
int mono_stack(int l, int r){
for(; st.size(); st.pop());
for(int i=l; i<r; i++){
for(; !st.empty()&&h[st.top()]>=h[i]; st.pop());
if(st.empty()) L[i]=l;
else L[i]=st.top()+;
st.push(i);
}
for(; st.size(); st.pop());
for(int i=r-; i>=l; i--){
for(; !st.empty() && h[st.top()]>=h[i]; st.pop());
if(st.empty()) R[i]=r;
else R[i]=st.top();
st.push(i);
}
int res=;
for(int i=l; i<r; i++)
res=max(res, h[i]*(R[i]-L[i]));
return res;
} void solve(int n, int m){
int res=;
for(int i=; i<n; i++){
if(i==) for(int j=; j<m; j++) h[j]=;
else for(int j=; j<m; j++)
if(a[i][j]==a[i-][j]) h[j]++;
else h[j]=;
//two-pointers
for(int l=, r; l<m; l=r){
for(r=l+; r<m && a[i][r]==a[i][l]; r++);
res=max(res, mono_stack(l, r));
}
}
printf("%d\n", res);
} int main(){
for(int n, m; ~scanf("%d%d", &n, &m); ){
for(int i=; i<n; i++)
for(int j=; j<m; j++)
scanf("%d", a[i]+j);
solve(n, m);
}
return ;
}

DLUTOJ 1033 Matrix的更多相关文章

  1. URAL 1033 Labyrinth

    E - Labyrinth Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submi ...

  2. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

  3. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  4. Atitit Data Matrix dm码的原理与特点

    Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...

  5. Android笔记——Matrix

    转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...

  6. 通过Matrix进行二维图形仿射变换

    Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...

  7. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  8. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  9. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. smarty缓存控制

    第一步初始化配置文件中设置 如果当前访问的模板有缓存就不需要连接数据库那些代码了,如果要模板局部不缓存,要写在iscache外,模板中用{nocache}

  2. 【C#】【Thread】上下文同步域SynchronizationAttribute

    上下文同步:使用SynchronizationAttribute为ContextBoundObject对象创建一个简单的自动的同步. 这种同步方式仅用于实例化的方法和域的同步.所有在同一个上下文域的对 ...

  3. Windows Phone:自定义字体在xaml和代码中使用

    最近,我的小应用<认字>更新了一个能发声的版本,朋友对Speech做读音没有兴趣,反而对其中使用的楷体文字表示了兴趣,也许Speech的文章比较多,这次我对这个自定义字体在xaml和代码中 ...

  4. [转]hive实例讲解实现in和not in子句

    FROM : http://www.cnblogs.com/ggjucheng/archive/2013/01/03/2842855.html 目前hive不支持 in或not in 中包含查询子句的 ...

  5. QT QT程序初练

    //界面编程#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) ...

  6. java内部类 2016年12月13号

    1.在外部类的任意位置创建内部类对象的方法: 1)从外部类的非静态方法之外的任意位置创建某个内部类的对象,必须指明这个对象所在的外部类和内部类:OuterClassName.InnerClassNam ...

  7. jdbc 得到表结构、主键

    jdbc 得到表结构.主键 标签: jdbctablenullschema数据库mysql 2012-02-16 22:13 11889人阅读 评论(0) 收藏 举报  分类: Java(71)  假 ...

  8. [CareerCup] 7.3 Line Intersection 直线相交

    7.3 Given two lines on a Cartesian plane, determine whether the two lines would intersect. 这道题说是在笛卡尔 ...

  9. 从Lumia退役看为什么WP走向没落

    前段时间决定将自己用了三年多的Lumia 800正式退役,这是我用的时间最长的手机,虽然系统上有缺陷,但是好不妨碍他成为我最有感情的一部手机.由于之前是WinPhone 开发者的关系,这部手机是微软送 ...

  10. bluebird

    nodejs-使用request和bluebird编写的http请求模块   http://blog.csdn.net/o6875461/article/details/44594545