Second Large Rectangle


题目大意:
给你一个N*M的01矩阵,问这个矩阵中存在的第二大的矩形面积是多少,要求矩形是由全1 组成的,注意,如果只存在一个这样的矩形,那么就输出0
分析:
算是提供一个思路吧,定义一个二维数组,通过这个二维数组来记录每一列连续的1的个数,然后再去枚举列
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
typedef long long ll;
using namespace std;
const int INF=0x3f3f3f3f;
int dp[1010][1010];
int n,m;
int max1,max2;
struct node{
int h,w;
node (int _h=0,int _w=0):h(_h),w(_w){};
}num[1010];
void cmp(int x){//max1是最大,max2是第二大
// cout<<x<<endl;
if(x>max1){
max2=max1;
max1=x;
}else{
max2=max(max2,x);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%1d",&dp[i][j]);
dp[i][j]+=dp[i][j]*dp[i-1][j];
}
}
for(int i=1;i<=n;i++)
{
int top=0,res;
for(int j=1;j<=m;j++){
if(!dp[i][j]) {//说明这个一列没有办法用了,那么就需要去从新去获得矩形的宽
top=0;
continue;
}
//下面这三行算是核心的算法了
res=j;//不是从1开始,应是从当前的列开始的
while(num[top].h>dp[i][j]&&top) res=num[top--].w;//构造出来的num数组是从小到大的数组
if(dp[i][j]!=num[top].h) num[++top]=node(dp[i][j],res);//增加新的宽度
for(int k=1;k<=top;k++){
cmp(num[k].h*(j-num[k].w+1));
}
}
}
printf("%d\n",max2);
return 0;
}
本文介绍了一个算法问题的解决方案,旨在寻找一个N*M的01矩阵中第二大的全1矩形面积。通过定义二维数组记录每列连续1的个数,再枚举列的方式,最终找到矩阵中第二大的矩形面积。
354

被折叠的 条评论
为什么被折叠?



