19 nowcoder 第二场 经典dp延伸,单调队列变量维护 H Second Large Rectangle

 题意: n*m大小的矩形  n,m<1000。 s[ i ][ j ] 只包含 0 和 1,求 只包含1 的第二大矩形面积。

 dp[i][j]表示向上能延伸的最大长度。  (不明白可以自己写个样例,打印dp

对每一行维护一个队列,如果 dp[i][j] >0就入队,当dp[i][j] 小于队尾元素,就开始进行出队操作

例如对于 1 1 0 1 1,   操作为 1 入队, 1入队, 0小于队尾1, 0下标 j = 3, 从后往前更新答案 ( j-1的下标2 )* 1能向上延伸最大长度1,  更新答案(j - 1的下标1)*1能向上延伸的最大长度

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1011;
int a[maxn][maxn],d[maxn][maxn];
int max_1,max_2;
char s[maxn][maxn];
int t[maxn],q[maxn];
void upda(int x){
	if(x>max_1){
		max_2 = max_1;
		max_1 = x;
	}else if(x>max_2){
		max_2 = x;
	}
}
int main()
{
	int n,m;
	scanf("%d %d",&n,&m);
	getchar();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			scanf("%c",&s[i][j]);
			if(s[i][j]=='1') d[i][j] = d[i-1][j]+1; 
			else d[i][j]=0;
		}
		getchar();
	}
	int r = 0;
	for(int i=1;i<=n;i++){
		r  = 0;
		for(int j=1;j<=m+1;j++){
            int l = j;
			while(r && d[i][j] < t[r] ){
				int x = t[r], y = j - q[r];
				upda(x*y);	upda((x-1)*y);	upda(x*(y-1));
				l = q[r];
				r--;
			}
			if(d[i][j]){
				q[++r] = l;
				t[r] = d[i][j]; 	
			}
		}
	}
	printf("%d\n",max_2);
	return 0;
}
/*
4 5
11011
11111
01100
01100

*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wym_king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值