poj 3494 Largest Submatrix of All 1’s (单调栈)

poj 3494

题意:给出一个n*m个0-1图,求最大的全部由1组成的矩阵。

思路:同poj 1964,一共做n+2×m次单调栈。。。数组开小re一发。。某处stk忘记清空re 1发。。。智力-2.。。3A

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年08月03日 星期三 19时33分14秒
 4File Name :code/poj/3494.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <stack>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27using namespace std;
28const double eps = 1E-8;
29const int dx4[4]={1,0,0,-1};
30const int dy4[4]={0,-1,1,0};
31const int inf = 0x3f3f3f3f;
32const int N=2E3+50;
33int maze[N][N];
34int a[N][N],l[N][N],r[N][N];
35stack<int>stk;
36int n,m;
37int main()
38{
39	#ifndef  ONLINE_JUDGE 
40	freopen("code/in.txt","r",stdin);
41  #endif
42	while (~scanf("%d%d",&n,&m))
43	{
44	    ms(maze,-1); //设置哨兵。。。
45	    ms(a,-1);
46	    while (!stk.empty()) stk.pop();
47	    for ( int i = 1 ; i <= n ; i++)
48		for ( int j = 1 ; j <= m ; j++)
49		    scanf("%d",&maze[i][j]);
50	    int x;
51	    for ( int i = 1 ; i <= n ; i++)
52	    {
53		while (!stk.empty()) stk.pop();
54		stk.push(m+1);
55		for ( int j = m ; j >=1 ; j--)
56		{
57		    for (x=stk.top() ; maze[i][x]==1 ; x = stk.top()) stk.pop();
58		    a[i][j] = x-j;
59		    if (maze[i][j]==0) a[i][j] = 0 ;
60		    stk.push(j);
61		}
62	    }
63/*	    for ( int i = 1 ;i <= n ; i++){
64		for ( int j = 1 ; j <= m ; j++)
65		    printf("%d hhh",a[i][j]);
66		printf("\n");
67	    }  */
68	    for ( int j = 1 ; j <= m ; j++)
69	    {
70		while (!stk.empty()) stk.pop();
71		stk.push(0);
72		for ( int i = 1 ; i <= n ; i++)
73		{
74		    for (x=stk.top() ; a[x][j]>=a[i][j] ; x=stk.top()) stk.pop();//cout<<"x:"<<x<<endl;
75		    l[i][j] = x+1;
76		    stk.push(i);
77		//    cout<<i<<endl;
78		}
79		while (!stk.empty()) stk.pop();
80		stk.push(n+1);
81		for ( int i = n ; i >=1 ; i--)
82		{
83		    for (x=stk.top() ; a[x][j]>=a[i][j] ; x = stk.top()) stk.pop();
84		    r[i][j] = x-1;
85		    stk.push(i);
86		}
87	    }
88	    int ans = 0 ;
89	    for ( int i = 1; i <= n ; i++)
90		for ( int j = 1 ; j <= m ; j++)
91		    ans = max(ans,(r[i][j]-l[i][j]+1)*a[i][j]);
92	    printf("%d\n",ans);
93	}
94  #ifndef ONLINE_JUDGE  
95  fclose(stdin);
96  #endif
97    return 0;
98}