传送门

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. dos系统下mysql常用命令

    show table status;//查看所有表状态,通过这个命令可以得知表的创建时间和最后更新时间,以及该表是基表还是视图以及是什么表引擎等信息. show table status from d ...

  2. 会报编译器警告的Xcode 6.3新特性:Nullability Annotations

    最近在用Xcode 6.3写代码,一些涉及到对象的代码会报如下编译器警告: 1 Pointer is missing a nullability type specifier (__nonnull o ...

  3. StackBlur.js

    StackBlur.js 是 Mario Klingemann 创建的一个快速的.接近高斯模糊的效果库. 更多信息: http://incubator.quasimondo.com/processin ...

  4. f2fs解析(一)f2fs如何解决wandering tree

    wandering tree问题是log-structured 文件系统(LFS) 特有的一个问题,因为LFS的脏数据是追加更新的,所以如果一个数据块变脏了,那么那个数据块的直接索引块.间接索引块都会 ...

  5. 3110 PHP常见问题

    1.中文显示 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&g ...

  6. Linux下用信号量实现对共享内存的访问保护

    转自:http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108768.html 最近一直在研究多进程间通过共享内存来实现通信的事情,以便高 ...

  7. js中的预加载与懒加载(延迟加载)

    js中加载分两种:预加载与延迟加载 一.  预加载,增强用户的体验,但会加载服务器的负担.一般会使用多种 CSS(background).JS(Image).HTML(<img />) . ...

  8. SignalR 实现web浏览器客户端与服务端的推送功能

    SignalR 是一个集成的客户端与服务器库,基于浏览器的客户端和基于 ASP.NET 的服务器组件可以借助它来进行双向多步对话. 换句话说,该对话可不受限制地进行单个无状态请求/响应数据交换:它将继 ...

  9. 解放双手:如何在本地调试远程服务器上的Node代码

    写在前面 谈到node断点调试,目前主要有三种方式,通过node内置调试工具.通过IDE(如vscode).通过node-inspector,三者本质上差不多.本文着重点在于介绍 如何在本地通过nod ...

  10. PHP PC端接入支付宝和微信感悟

    想着中秋节的时候把异步线程学习完,同事说有个PHP的支付要帮忙做一下,虽然中秋节计划是把C#的异步学完,不过还是喜欢挑战,好久没有像大学一样这么认真的熬夜解决问题了.由于在大学学过asp,它和php有 ...