poj3494(单调栈)

传送门

ac代码:

#include<bits/stdc++.h>
#define per(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
//#define int long long
const ll inf =2333333333333333LL;
const double eps=1e-8;
int read(){
    char ch=getchar();
    int res=0,f=0;
    while(ch<'0' || ch>'9'){f=(ch=='-'?-1:1);ch=getchar();}
    while(ch>='0'&&ch<='9'){res=res*10+(ch-'0');ch=getchar();}
    return res*f;
}
// ------------------------head
#define mod 1000000007
const int N=2005;
int data[N][N],L[N],R[N],n,m;

signed main()
{
    while(scanf("%d%d",&m,&n)!=EOF){
        per(i,1,m)per(j,1,n)
            scanf("%d",&data[i][j]);
        per(j,1,n){
            int cnt=0;
            per(i,1,m){
                if(data[i][j]==0)cnt=0;
                else if(data[i][j]==1){
                    data[i][j]=++cnt;
                }
            }
        }
        stack<int>st;
        int res=0;
        for(int i=1;i<=m;i++){
            while(!st.empty())st.pop();
            per(j,1,n){
                while(!st.empty()&&data[i][j]<=data[i][st.top()])st.pop();
                if(st.empty())L[j]=0;
                else L[j]=st.top();
                st.push(j);
            }
            while(!st.empty())st.pop();
            for(int j=n;j>=1;j--){
                while(!st.empty()&&data[i][j]<=data[i][st.top()])st.pop();
                if(st.empty())R[j]=n;
                else R[j]=st.top()-1;
                st.push(j);
            }
            per(j,1,n){
                res=max(res,data[i][j]*(R[j]-L[j]));
            }
        }
        printf("%d\n",res);
    }
    return 0;
}

 

posted @ 2018-09-17 22:02  WindFreedom  阅读(498)  评论(0)    收藏  举报