Dynamic Programming Training bis

Dynamic Planning Special
dynamic programming Theme II:

Hanging line method

What is suspended line method

Suspended line is defined like this:

From every point to go up, know the obstacles or the top point boundary.

Then we can easily get some of the properties of the suspension lines of:

  1. Each point corresponds to a suspended line
  2. Corresponding to each of the suspension wires have a height equal to the height of the suspended line, is greater than the width of the rectangle 0

Therefore, the step is suspended line method: to find a point corresponding to the height of each of the suspension wires, and then find the width of the suspension wires to extend a rectangular right and left, respectively.

example:

https://www.luogu.org/problem/P1169

Man of few words said, the idea is similar to the largest sub-matrix and the code is well understood, very clear routine

special:

  1. c ++ iostream has a left function, so please do not quote bits / stdc ++. h or iostream
  2. Rectangle is a square area demand shorter side length of the square

See Code ( anti-copying operation oh )

code by std:

#include<cstdio>
#define N 2005
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
using namespace std;
int up[N][N],left[N][N],right[N][N],ansa,ansb,a[N][N],m,n;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            up[i][j]=1,left[i][j]=j,right[i][j]=j,scanf("%d",&a[i][j]);
    for(int i=1;i<=n;i++)
        for(int j=2;j<=m;j++)
            if(a[i][j]^a[i][j-1])
                left[i][j]=left[i][j-1];
    for(int i=1;i<=n;i++)
        for(int j=m;j>1;j--)
            if(a[i][j]^a[i][j-1])
                right[i][j-1]=right[i][j];//left/right初值,即(i,j)点向左/右的最大宽度

    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(i>1&&a[i][j]^a[i-1][j])
                up[i][j]=up[i-1][j]+1,left[i][j]=max(left[i][j],left[i-1][j]),
                right[i][j]=min(right[i][j],right[i-1][j]);
            int a=right[i][j]-left[i][j]+1;
            int b=min(a,up[i][j]);
            ansa=max(ansa,b*b);
            ansb=max(ansb,a*up[i][j]);
        }
    printf("%d\n%d",ansa,ansb);
} 

Guess you like

Origin www.cnblogs.com/wzxbeliever/p/11622576.html
Recommended