Hanging line method (knowledge summary)

Summer school, then I did not sum up, today they see that the dreaded forget ......

In fact, it is still dp thoughts, just kept on going like this ......

Ideas sources

https://blog.csdn.net/qq_30358129/article/details/88044727

Maximum 01 sub-matrix

Questions can be used to pay poj3494

h [i] [j] expressed as (i, j) is the height of the suspension wires lowermost position,

l [i] [j] represents the (i, j) can Extension to the leftmost position, r [i] [j] Representative (i, j) can be the rightmost position to the extension

Enumerate each line, then the line if there is value, then the child will be peer rectangular narrow, otherwise unchanged

More than 01 ideas you can solve the biggest problem of sub-matrices, then monotonous stack can be resolved

#include<iostream>
#include<cstdio>
using namespace std;
const int N=2e3+10;
int m,n,a[N][N],l[N][N],r[N][N],h[N][N];
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=m;++j)
			{
				scanf("%d",&a[i][j]);
			}
		}
		int L=1e9,R=0,ans=0;
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=m;++j)
			{
				if(a[i][j]==1)L=min(L,j);//尺取连续段最左1的位置 
				else L=1e9;
				l[i][j]=L;
			}
			for(int j=m;j>=1;--j)
			{
				if(a[i][j]==1)R=max(R,j);//尺取连续段最右1的位置 
				else R=0;
				r[i][j]=R; 
			}
			for(int j=1;j<=m;++j)
			{
				if(a[i-1][j])
				{
					h[i][j]=h[i-1][j]+1;
					l[i][j]=max(l[i-1][j],l[i][j]);//收左线
					r[i][j]=min(r[i-1][j],r[i][j]);//收右线 
				}
				else h[i][j]=1;
				if(a[i][j])ans=max(ans,(r[i][j]-l[i][j]+1)*h[i][j]);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

The maximum matrix with dice

The change of the suspension lines magic method, to pull out the same color, when considering modified L [] and R & lt [] value

Code h [j] is initialized to the minimum / maximum, so as to take the max and min modifications take unified L [] and R & lt [] is not updated when

The same idea is that, if the above can be inherited, it will shrink the left boundary and the right boundary, on behalf of the suspension lines will take about community

If you still monotonous stack, the need to set a foot to take

#include<iostream>
#include<cstdio>
using namespace std;
const int N=2e3+10;
typedef long long ll;
int m,n,a[N][N];
int l[N],r[N],h[N];
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=m;++j)
			{
				scanf("%d",&a[i][j]);
			}
		}
		for(int j=1;j<=m;j++)
			l[j]=0,r[j]=m+1,h[j]=0;
		ll ans=0;
		int la,ra;
		for(int i=1;i<=n;++i)
		{
			la=0;ra=m+1;
			for(int j=1;j<=m;++j)
			{
				if(a[i][j]==a[i-1][j])
					h[j]++;
				else 
					h[j]=1,l[j]=0,r[j]=m+1;
				if(a[i][j]==a[i][j-1])
					l[j]=max(l[j],la+1);
				else 
					l[j]=j,la=j-1;//考虑到l[j]<=j而此处l[j]只能等于j
			}
			for(int j=m;j>=1;--j)
			{
				if(a[i][j]==a[i][j+1])
					r[j]=min(r[j],ra-1);
				else 
					r[j]=j,ra=j+1;//同理
				ans=max(ans,1ll*h[j]*(r[j]-l[j]+1)*a[i][j]);
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

 

Published 467 original articles · won praise 53 · views 40000 +

Guess you like

Origin blog.csdn.net/Code92007/article/details/103460133