Codeforces Round #578 (Div. 2) D. White Lines

D. White Lines 传送门

time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of n rows and n columns of square cells. The rows are numbered from 1 to n, from top to bottom, and the columns are numbered from 1 to n, from left to right. The position of a cell at row r and column c is represented as (r,c). There are only two colors for the cells in cfpaint — black and white.

There is a tool named eraser in cfpaint. The eraser has an integer size k (1≤k≤n). To use the eraser, Gildong needs to click on a cell (i,j) where 1≤i,j≤n−k+1. When a cell (i,j) is clicked, all of the cells (i′,j′) where i≤i′≤i+k−1 and j≤j′≤j+k−1 become white. In other words, a square with side equal to k cells and top left corner at (i,j) is colored white.

white line is a row or a column without any black cells.

Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question.

Input

The first line contains two integers n and k (1≤k≤n≤2000) — the number of rows and columns, and the size of the eraser.

The next n lines contain n characters each without spaces. The j-th character in the i-th line represents the cell at (i,j). Each character is given as either 'B' representing a black cell, or 'W' representing a white cell.

Output

Print one integer: the maximum number of white lines after using the eraser exactly once.

Examples

input

4 2

BWWW

WBBW

WBBW

WWWB

output

4

input

3 1

BWB

WWB

BWB

output

2

input

5 3

BWBBB

BWBBB

BBBBB

BBBBB

WBBBW

output

2

input

2 2

BW

WB

output

4

input

2 1

WW

WW

output

4

Note

In the first example, Gildong can click the cell (2,2), then the working screen becomes:

BWWW
WWWW
WWWW
WWWB

Then there are four white lines — the 2-nd and 3-rd row, and the 2-nd and 3-rd column.

In the second example, clicking the cell (2,3) makes the 2-nd row a white line.

In the third example, both the 2-nd column and 5-th row become white lines by clicking the cell (3,2).

 

题意:

给你一个n*n的矩阵,里面有两种字符,‘W’和‘B’,代表black 和white 。其实这个矩阵就是一个方形画板,你有一个k*k的橡皮只能用一次,使k*k的矩阵里的B变成W,问完全空白的行和列的总数?

思路:

用1代替B,0代替W,然后维护一个前缀和数组,看能否用一个橡皮的操作使这一列或行的前缀和变为0,然后维护答案就好了,具体操作可以把列对称成行,相当于搞两个不同的数组,这样就只需要搞两个数组的行就可以了。如图:

 

 剩下的具体思路看代码吧

如下:

#include<bits/stdc++.h>
using namespace std;
const int N=2010;
int n,k,ans,tot,r[N][N],c[N][N],okr[N][N],okc[N][N];
char mp[N][N];
int main()
{
    scanf("%d%d%*c",&n,&k);
    for(int i=1;i<=n;scanf("%*c"),i++)
        for(int j=1;j<=n;j++)
            scanf("%c",&mp[i][j]);
//  输入 
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            r[i][j]=r[i][j-1]+(mp[i][j]=='B'),c[i][j]=c[i][j-1]+(mp[j][i]=='B');
//  记录 1代表'B',0代表'W';
//  这里将数组关于斜对角线对称一下,问题就变成了统计对称前擦除操作后有多少个全0的行!
//  和对称后的那个数组擦除操作后有多少的全0的行(因为对称后列变成了行)如上图 
    for(int i=1;i<=n;i++)tot+=(r[i][n]==0)+(c[i][n]==0);
// tot是没有擦除操作就是全0行的个数 
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n-k+1;j++)
            okr[i][j]=(r[i][j+k-1]-r[i][j-1]==r[i][n]&&r[i][n]!=0)+okr[i-1][j],
            //这一行中只有长度为k的区间有1等价于【(r[i][j+k-1]-r[i][j-1]==r[i][n]】
            // 【r[i][n]!=0】避免重复计算初始为全0行的个数 
            //那么可以通过擦除操作对答案做出贡献 ,并作一个前缀和 
            okc[i][j]=(c[i][j+k-1]-c[i][j-1]==c[i][n]&&c[i][n]!=0)+okc[i-1][j];
            //同理 
    for(int i=1;i<=n-k+1;i++)
        for(int j=1;j<=n-k+1;j++)
            ans=max(ans,tot+okr[i+k-1][j]-okr[i-1][j]+okc[j+k-1][i]-okc[j-1][i]);
            //维护一个初始全0行+操作后对答案贡献的两个区间和  
    printf("%d",ans);
}

PS:感谢codeforces上wushuhan用户,此题解学习了大佬的代码58607058,感谢感谢~~

posted @ 2019-08-12 14:12  GeraldG  阅读(276)  评论(0编辑  收藏  举报