Two-dimensional differential prefix and --cf1202D (good title)

Direct enumeration of each point as the upper-left corner that can be done, but more difficult to write

There is a simpler approach is statistical contribution to a column or row

B is present such that a line section L, R then there are three cases

  1. There is no such interval in which the line is W, the contribution of this line at this time the answer has been 1

  2.R-L + 1 <= k, then this section must find rectangle to cover a representative point can be determined in the presence of such points section is a rectangle, and only if, when points within the rectangle, this line will contribute 1

  3.R-L + 1> k, never contribute

For Case 2, we used a two-dimensional differential to look at statistics, last enumerated each point, we choose to see this point on behalf of the rectangle, if the maximum contribution on the line

#include<bits/stdc++.h>
using namespace std;
#define N 2005
char mp[N][N];
int n,k,tot,l[N],r[N],u[N],d[N],cnt[N][N];
int main(){
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            scanf("\n%c",&mp[i][j]);
    
    memset(l,0x3f,sizeof l);
    memset(u,0x3f,sizeof u);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            if(mp[i][j]=='B')
                l[i]=min(l[i],j),r[i]=max(r[i],j);
        if(l[i]==0x3f3f3f3f)
            tot++;
        else if(r[i]-l[i]+1<=k){
            int x1=max(1,i-k+1),y1=max(1,r[i]-k+1);
            int x2=i,y2=l[i];
            cnt[x1][y1]++;cnt[x1][y2+1]--;
            cnt[x2+1][y1]--;cnt[x2+1][y2+1]++;
        }
    }
    for(int j=1;j<=n;j++){
        for(int i=1;i<=n;i++)
            if(mp[i][j]=='B')
                u[j]=min(u[j],i),d[j]=max(d[j],i);
        if(u[j]==0x3f3f3f3f)
            tot++;
        else if(d[j]-u[j]+1<=k){
            int x1=max(1,d[j]-k+1),y1=max(1,j-k+1);
            int x2=u[j],y2=j;
            cnt[x1][y1]++;cnt[x1][y2+1]--;
            cnt[x2+1][y1]--;cnt[x2+1][y2+1]++;
        }
    }
    
    intyears = 0 ;
    for ( int i = 1 ; i <= n; i ++ )
         for ( int j = 1 ; j <= n; j ++ ) { 
            cnt [i] [j] + = cnt [i- 1 ] [j] + cnt [ i] [j- 1 ] -cnt [i- 1 ] [j- 1 ]; 
            years = max (years cnt [i] [j]); 
        } 
    Cout << + years earlier << endl; 
    
}

 

Guess you like

Origin www.cnblogs.com/zsben991126/p/11518012.html