BZOJ1057: [ZJOI2007]棋盘制作(洛谷P1169)

DP

BZOJ题目传送门
洛谷题目传送门

这是一类套路题,叫最大子矩形问题,具体可以看看这里

代码:

#include
#include
#include
#include
#define N 2005
#define F inline
using namespace std;
int n,m,ans1,ans2,h[N][N],l[N][N],r[N][N],L[N][N],R[N][N],s[N][N];
bool f[N][N];
F char readc(){
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    if (l==r) return EOF; return *l++;
}
F int _read(){
    int x=0; char ch=readc();
    while (!isdigit(ch)) ch=readc();
    while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
    return x;
}
F void doit(){
    memset(h,0,sizeof(h)),memset(s,0,sizeof(s));
    memset(L,0,sizeof(L)),memset(R,0,sizeof(R));
    memset(l,0,sizeof(l)),memset(r,0,sizeof(r));
    for (int i=1;i<=n;i++){
        for (int j=1,tem=0;j<=m;j++)
            if (f[i][j]) L[i][j]=tem;
            else tem=j,l[i][j]=0;
        for (int j=m,tem=m;j;j--)
            if (f[i][j]) R[i][j]=tem;
            else tem=j,r[i][j]=m;
        for (int j=1;j<=m;j++) r[0][j]=m;
            for (int j=1;j<=m;j++)
                if (f[i][j]){
                    h[i][j]=h[i-1][j]+1;
                    l[i][j]=max(L[i][j],l[i-1][j]);
                    r[i][j]=min(R[i][j],r[i-1][j]);
                    s[i][j]=min(s[i-1][j-1],min(s[i-1][j],s[i][j-1]))+1;
                    ans1=max(ans1,s[i][j]*s[i][j]);
                    ans2=max(ans2,(r[i][j]-l[i][j]-1)*h[i][j]);
                }
    }
}
int main(){
    n=_read(),m=_read();
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++){
            f[i][j]=_read(); 
            if ((i+j)%2==0) f[i][j]^=1;
        }
    doit();
    for (int i=1;i<=n;i++) 
        for (int j=1;j<=m;j++) 
            f[i][j]^=1;
    doit();
    return printf("%d\n%d\n",ans1,ans2),0;
}

你可能感兴趣的:(洛谷,BZOJ,DP---一般DP,蒟蒻zxl的Blog专栏)