DLUTOJ 1033 Matrix
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
Sample Output
1
4
HINT
Source
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的更多相关文章
- URAL 1033 Labyrinth
E - Labyrinth Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submi ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- 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 ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 日期选择器:jquery datepicker的使用
helloweba.com 作者:月光光 时间:2012-04-08 21:05 标签: jquery datepicker jquery ui 在jquery ui中,提供了一个非常实用 ...
- Cursor的各种效果
总结之后的Cursor的各种效果: http://sandbox.runjs.cn/show/bbwoyn0c http://css-cursor.techstream.org/ 源代码如下: < ...
- RDLC系列之三 图片显示
一.头像效果
- [转]hive实例讲解实现in和not in子句
FROM : http://www.cnblogs.com/ggjucheng/archive/2013/01/03/2842855.html 目前hive不支持 in或not in 中包含查询子句的 ...
- 使用地址栏访问CXF Webservice写法
/* * 通过url调用 * http://localhost:8080/EFP/webService/TestWebservice/testOut/arg0/liuyx */ http://loca ...
- android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类)
在使用android-async-http的时候我的apl 更新到了23,我的build version也是23的时候出现了,org.apache.http.Header这个类找不到的情况,原因是在a ...
- Django实际站点项目开发经验谈
开发了两个月的Django站点正式上线了,看着网站从无到有,从前端到后台,从本地开发到环境部署,一点一滴的堆砌成型,着实带给我不小的乐趣. Django站点介绍: 开发环境:阿里云服务器centos6 ...
- 从实用主义深入理解c++虚函数
记得几个月前看过C++虚函数的问题,当时其实就看懂了,最近笔试中遇到了虚函数竟然不太确定,所以还是理解的不深刻,所以想通过这篇文章来巩固下. 装逼一刻: 最近,本人思想发生了巨大的转变,在大学的时候由 ...
- angular(常识)
我觉得angularjs是前端框架,而jquery只是前端工具,这两个还是没有可比性的. 看知乎上关于jquery和angular的对比http://www.zhihu.com/question/27 ...
- linux之条件判断
一.文件类型比较 判断一个文件是否存在(注意:中括号表示判断,其两边必须有空格) 二.文件权限判断 判断是否有可执行权限(这里是不区分用户的,只要该文件能执行就返回0) 3.整数比较 判断两个数是否相 ...