题目:给你一个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的更多相关文章

  1. UVa 10667 - Largest Block

    题目大意:这个也是和UVa 836 - Largest Submatrix差不多,修改一下数据就可以套用代码的. #include <cstdio> #include <cstrin ...

  2. Largest Submatrix(动态规划)

    Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. POJ-3494 Largest Submatrix of All 1’s (单调栈)

    Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 8551   Ac ...

  4. hdu 2870 Largest Submatrix(平面直方图的最大面积 变形)

    Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change ...

  5. 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 ...

  6. codeforces 407D Largest Submatrix 3

    codeforces 407D Largest Submatrix 3 题意 找出最大子矩阵,须满足矩阵内的元素互不相等. 题解 官方做法 http://codeforces.com/blog/ent ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Subsequence(两个单调队列)

    Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. Direct3D 11的Device接口和DeviceContext接口

    D3D的两个主要的接口: Device,ID3D11Device.创建资源,Shader对象,状态对象,查询对象,等.以及检查硬件功能,调试函数.可以认为是资源的提供者. Device Context ...

  3. Android应用程序安装过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6766010 Android系统在启动的过程中, ...

  4. EffectiveC#16--垃圾最小化

    1.申请和释放一个基于堆内存的对象要花上更多的处理器时间. 所以当一个引用类型的局部变量在常规的函数调用中使用的非常频繁时应该把它提升为对象的成员(方法一) 2.当你把一个实现了IDisposable ...

  5. Emit技术使用实例及应用思路

    System.Reflection.Emit提供了动态创建类并生成程序集的功能. 适用于.NET Framework 2.0及其以后的版本. 动态生成类在对于O/R Mapping来说有很大的作用,在 ...

  6. iOS-设计模式之通知

    通知设计模式简单好用,就是一个项目中如果用的太多,不利于代码维护,可读性太差. 实现过程: [[NSNotificationCenter defaultCenter]postNotificationN ...

  7. VS C# 嵌入互操作类型

    SQLDMO.Restore oRestore = new SQLDMO.RestoreClass(); SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLSer ...

  8. 复数类(C++练习一)

    写一个复数类,实现基本的运算,目的熟悉封装与数据抽象. 类的定义 #include <iostream> #include <vector> using namespace s ...

  9. 图的割点 桥 双连通(byvoid)

    [点连通度与边连通度] 在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合.一个图的点连通度的定义为,最小割点集 ...

  10. (原)ubuntu上安装dlib

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5652791.html 参考网址: http://stackoverflow.com/questions ...