BZOJ1057【单调栈】

来源:互联网 发布:淘宝没有3c会怎么样 编辑:程序博客网 时间:2024/06/09 13:07

无限仰慕KeyID讲的不用单调栈的方法.

/* I will wait for you */#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;const int maxn=2010;const int maxm=1010;const int maxs=26;const int INF=1<<29;const int P=1000000007;const double error=1e-9;inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') f=(ch=='-'?-1:1),ch=getchar();while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;}int n,m,ansa,ansb,ma[maxn][maxn],h0[maxn][maxn],h1[maxn][maxn],l[maxn],r[maxn],q[maxn];int main(){n=read(),m=read();for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ma[i][j]=read();for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)h0[i][j]=ma[i][j]?0:h1[i-1][j]+1,h1[i][j]=ma[i][j]?h0[i-1][j]+1:0;for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if((i+j)&1) swap(h0[i][j],h1[i][j]);for(int i=1;i<=n;i++){for(int j=1,top=0;j<=m;j++){while(top&&h0[i][q[top]]>=h0[i][j]) top--;l[j]=top?q[top]+1:1,q[++top]=j;}for(int j=m,top=0;j>=1;j--){while(top&&h0[i][q[top]]>=h0[i][j]) top--;r[j]=top?q[top]-1:m,q[++top]=j;}for(int j=1;j<=m;j++) {ansa=max(ansa,h0[i][j]*(r[j]-l[j]+1));ansb=max(ansb,min(h0[i][j],r[j]-l[j]+1)*min(h0[i][j],r[j]-l[j]+1));}}for(int i=1;i<=n;i++){for(int j=1,top=0;j<=m;j++){while(top&&h1[i][q[top]]>=h1[i][j]) top--;l[j]=top?q[top]+1:1,q[++top]=j;}for(int j=m,top=0;j>=1;j--){while(top&&h1[i][q[top]]>=h1[i][j]) top--;r[j]=top?q[top]-1:m,q[++top]=j;}for(int j=1;j<=m;j++) {ansa=max(ansa,h1[i][j]*(r[j]-l[j]+1));ansb=max(ansb,min(h1[i][j],r[j]-l[j]+1)*min(h1[i][j],r[j]-l[j]+1));}}printf("%d\n%d\n",ansb,ansa);return 0;}

0 0
原创粉丝点击