Largest Submatrix of All 1’s POJ - 3494 (单调栈 poj 2559升级版)

POJ 2559 AC:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long 
#define mem(a, b) memset(a, b, sizeof(a))
#define N 100005 
#define MOD
using namespace std;
const int inf=1<<29;
int n, p;
LL a[N], s[N], w[N];
int main() {
	while(scanf("%d",&n)!=EOF && n) {
		mem(a, 0), mem(s, 0), mem(w, 0);
		for(int i=1; i<=n; i++) scanf("%lld",&a[i]);
		a[n+1]=p=0;
		LL ans=0; 
		for(int i=1; i<=n+1; i++) {
			if(a[i]>s[p]) {
				s[++p]=a[i], w[p]=1;
			}
			else {
				int width=0;
				while(s[p]>a[i]) {
					width+=w[p];
					ans=max(ans, (LL)width*s[p]);
					p--;
				}
				s[++p]=a[i], w[p]=width+1;
			}
		}
		cout<<ans<<endl;
	}
}

POJ 3439:
预处理一下,以每一行为矩形的底

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long 
#define mem(a, b) memset(a, b, sizeof(a))
#define N 2010 
#define MOD
using namespace std;
int n, m, a[N][N];
int st[N], p, w[N];
int main() {
	while(scanf("%d%d",&n, &m)!=EOF){
		mem(a, 0);
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=m; j++)
				scanf("%d", &a[i][j]);
		}
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=m; j++){
				if(a[i][j]==1)
					a[i][j]=a[i-1][j]+1;
			}		
		}
		int ans=0;
		for(int i=1; i<=n; i++) {
			a[i][m+1]=p=0;
			for(int j=1; j<=m+1; j++) {
				if(a[i][j]>st[p]) {
					st[++p]=a[i][j], w[p]=1;
				}
				else {
					int width=0;
					while(st[p]>a[i][j]) {
						width+=w[p];
						ans=max(ans, width*st[p]);
						p--;
					}
					st[++p]=a[i][j], w[p]=width+1;
				}
			}
		}
		cout<<ans<<endl;
	}
}

你可能感兴趣的:(Largest Submatrix of All 1’s POJ - 3494 (单调栈 poj 2559升级版))