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 ...
随机推荐
- windows客户机连接gerrit的一个报错处理
gerrit环境部署在linux服务器,windos客户机连接gerrit进行代码操作: 在windows客户机下载Git客户端 在“Git Bash”里使用 ”ssh-keygen -t rsa - ...
- 关联:objc_getAssociatedObject和objc_setAssociatedObject使用
为UIButton的category添加属性 UIButton+subTitle.h #import <UIKit/UIKit.h> #import <objc/runtime.h& ...
- 关于audio元素在实际项目中遇到的问题总结
在ios高版本的微信浏览器下(ios10.0以上),audio标签如果添加autoplay属性的话.导致的问题是:通过二维码扫码第一次进入没有问题,第二次扫码进入之后直接卡死在loading页面. 解 ...
- C/C++中的结构体
结构体定义 结构体(struct)是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构. 结构体作用 结构体和其他类型基础数据类型一样,例如int类型,char类型 只不过结构体可以做成 ...
- UltraEdit编辑器使用心得之正则表达式篇
ultraEdit 中通过Ctrl+R 可以快速进行文本替换等处理操作,如果在这中间用一些正则表达式那将帮助NI更高效的进行文字处理操作,相关正则表达式列述如下: % 匹配行首 - 表示搜索字符串必须 ...
- Java获取客户端IP
在开发工作中,我们常常需要获取客户端的IP.一般获取客户端的IP地址的方法是:request.getRemoteAddr();但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实 ...
- Android -- 桌面悬浮,仿360
实现原理 这种桌面悬浮窗的效果很类似与Wid ...
- MMDrawerController第三方库的使用(根据导航item+滚动条progressView实现的手势滑动切换视图的)
https://github.com/mutualmobile/MMDrawerController MMDrawerControlleris边抽屉导航容器视图控制器用来支持越来越多的应用程序利用抽屉 ...
- offsetWidth与scrollLeft
有两个值一个是:scrollTop一个是scrollLeft第一个代表页面利用滚动条滚动到下方时,隐藏在滚动条上方的页面的高度:第二个代表页面利用滚动条滚动到右侧时,隐藏在滚动条左侧的页面的宽度 do ...
- sql server快速删除整个数据库表和存储过程
情况:在远程数据库删除表执行太慢,表过多,数据库无权删除 结果:保留空数据库 方法:利用sql语句,查询网络文摘解决. 说明: 有些有约束,不能直接delete,需要先删除所有约束,语句: DECLA ...