POJ 3494 Largest Submatrix of All 1’s
HDU 1506 Largest Rectangle in a Histogram
和这两题一样的方法。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=2000+10; int a[maxn]; int L[maxn],R[maxn]; int m,n; int tmp[maxn][maxn],b[maxn][maxn]; void f() { for(int i=1; i<=n; i++) L[i]=i; for(int i=2; i<=n; i++) { if(a[i]>a[i-1]) continue; int pre=L[i-1]; while(1) { L[i]=pre; if(pre==1||a[pre-1]<a[i]) break; pre=L[pre-1]; } } for(int i=n; i>=1; i--) R[i]=i; for(int i=n-1; i>=1; i--) { if(a[i]>a[i+1]) continue; int pre=R[i+1]; while(1) { R[i]=pre; if(pre==n||a[pre+1]<a[i]) break; pre=R[pre+1]; } } } int main() { while(~scanf("%d%d",&n,&m)) { memset(b,0,sizeof b); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) scanf("%d",&tmp[i][j]); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { if(tmp[i][j]==0) continue; b[i][j]=b[i][j-1]+tmp[i][j]; } int ans=0; for(int j=1;j<=m;j++) { for(int i=1;i<=n;i++) a[i]=b[i][j]; f(); for(int i=1;i<=n;i++) ans=max(ans,a[i]*(R[i]-L[i]+1)); } printf("%d\n",ans); } return 0; }