UVa 836 - Largest Submatrix
题目:给你一个n*n的01矩阵,求里面最大的1组成的矩形的米娜及。
分析:dp。单调队列。UVa 1330同题,仅仅是输入格式变了。
我们将问题分解成最大矩形。即求解以k行为底边的图形中的最大矩形。然后合并。求最大的矩形。
预处理: 求出以每行为底边的每一列从底边開始向上的最大连续1的高度MaxH。
O(N^2) ;
dp:对于每一层底边。我们利用单调队列求解出本行的最大矩形。 O(N)。
关于单调队列的求解分析,可參照zoj1985的题解;
整体时间:T(N) = O(N^2)+O(N)*O(N) = O(N^2)。
说明:注意数据读入格式。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio> using namespace std; char maps[30][30];
char Maps[30][30];
int MaxH[30][30];
int L[30],R[30];
int MUQ[30]; int main()
{
int T;
scanf("%d",&T);getchar();
for (int t = 1 ; t <= T ; ++ t) {
scanf("%s",maps[0]);
int n = strlen(maps[0]);
for (int i = 1 ; i < n ; ++ i)
scanf("%s",maps[i]);
for (int i = 1 ; i <= n ; ++ i)
for (int j = 1 ; j <= n ; ++ j)
Maps[i][j] = maps[i-1][j-1]; //计算每条底边上的每列高度
memset(MaxH, 0, sizeof(MaxH));
for (int i = 1 ; i <= n ; ++ i)
for (int j = 1 ; j <= n ; ++ j)
if ( Maps[i][j] == '1' )
MaxH[i][j] = MaxH[i-1][j]+1;
else
MaxH[i][j] = 0; for (int i = 1 ; i <= n ; ++ i)
MaxH[i][0] = MaxH[i][n+1] = -1; int MaxV = 0;
for (int i = 1 ; i <= n ; ++ i) {
//计算每一个点的左边界
int tail = 0;
MUQ[0] = 0;
for (int j = 1 ; j <= n+1 ; ++ j) {
while (tail >= 0 && MaxH[i][MUQ[tail]] > MaxH[i][j])
R[MUQ[tail --]] = j;
MUQ[++ tail] = j;
}
//计算每一个点的右边界
tail = 0;
MUQ[0] = n+1;
for (int j = n ; j >= 0 ; -- j) {
while (tail >= 0 && MaxH[i][MUQ[tail]] > MaxH[i][j])
L[MUQ[tail --]] = j;
MUQ[++ tail] = j;
}
//求解
for (int j = 1 ; j <= n ; ++ j) {
int Temp = MaxH[i][j]*(R[j]-L[j]-1);
if (MaxV < Temp)
MaxV = Temp;
}
}
printf("%d\n",MaxV);
if (t < T) printf("\n");
} return 0;
}
UVa 836 - Largest Submatrix的更多相关文章
- UVa 10667 - Largest Block
题目大意:这个也是和UVa 836 - Largest Submatrix差不多,修改一下数据就可以套用代码的. #include <cstdio> #include <cstrin ...
- Largest Submatrix(动态规划)
Largest Submatrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- POJ-3494 Largest Submatrix of All 1’s (单调栈)
Largest Submatrix of All 1’s Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8551 Ac ...
- hdu 2870 Largest Submatrix(平面直方图的最大面积 变形)
Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change ...
- Largest Submatrix of All 1’s
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we m ...
- codeforces 407D Largest Submatrix 3
codeforces 407D Largest Submatrix 3 题意 找出最大子矩阵,须满足矩阵内的元素互不相等. 题解 官方做法 http://codeforces.com/blog/ent ...
- Largest Submatrix of All 1’s(思维+单调栈)
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1's which is the largest? By largest we m ...
- POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈
POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ...
- POJ - 3494 Largest Submatrix of All 1’s 单调栈求最大子矩阵
Largest Submatrix of All 1’s Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is ...
随机推荐
- List数据集动态排序
List<Type> datas = new List<Type>; datas = datas.OrderBy(c => c.GetType().GetProperty ...
- 用gitolite新建项目,clone后首次push,可能会出现: git: No refs in common and none specified; doing no
用gitolite新建项目,clone后首次push,可能会出现: $ git push No refs in common and none specified; doing nothing ...
- 黑马程序员 ——Java SE(1)
----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训 ...
- Html5移动端页面布局通用模板暨移动端问题总结
最近的移动端项目终于告一段落了,其中遇到了不少问题,在此和大家总结分享. 首先,说一下结构.一般的手机页面大致可以分为五块:head, content, foot,solidbar,dialog. 针 ...
- C# 让textbox 只能输入数字的方法
使用textBox控件的KeyPress事件 private void textBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.Key ...
- Transform 1
Transform字面上就是变形,改变的意思.在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动translate以及矩阵变形matrix.下面我们一 ...
- UVA 221 - Urban Elevations(离散化)!!!!!!
题意:给出一张俯视图.给出N个建筑物的左下标,长度,宽度,高度.现在求,从南面看,能看到那些建筑? Sample Input 14 160 0 30 60 30 125 0 32 28 60 95 0 ...
- Java中的IO学习总结
今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬盘.内存.键盘等处理 ...
- JQuery validator扩展
//validator 扩展 jQuery.validator.addMethod("mail", function(value, element, messages) { ret ...
- 逛园子,看到个练习题,小试了一把(淘宝ued的两道小题)
闲来无事,逛园子,充充电.发现了一个挺有意思的博文,自己玩了一把. 第一题:使用 HTML+CSS 实现如图布局,border-widht 1px,一个格子大小是 60*60,hover时候边框变为橘 ...