AtCoder Beginner Contest 086 D - Checker

D - Checker


Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K. Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square. Below is an example of a checked pattern of side 3:

cba927b2484fad94fb5ff7473e9aadef.png

AtCoDeer has N desires. The i-th desire is represented by xiyi and ci. If ci is B, it means that he wants to paint the square (xi,yi) black; if ci is W, he wants to paint the square (xi,yi) white. At most how many desires can he satisfy at the same time?

Constraints

  • 1  N  105
  • 1  K  1000
  • 0  xi  109
  • 0  yi  109
  • If i  j, then (xi,yi)  (xj,yj).
  • ci is B or W.
  • NKxi and yi are integers.

Input

Input is given from Standard Input in the following format:

N K
x1 y1 c1
x2 y2 c2
:
xN yN cN

Output

Print the maximum number of desires that can be satisfied at the same time.


Sample Input 1

Copy
4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

Copy
4

He can satisfy all his desires by painting as shown in the example above.


Sample Input 2

Copy
2 1000
0 0 B
0 1 W

Sample Output 2

Copy
2

Sample Input 3

Copy
6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

Copy
4

题意:在一个黑块和白块交错的二维空间里(每个黑块和白块的边长为k),有一个二维直角坐标系(原点不确定),现在有n个点,每个点有相应的颜色,问最多有多少个点颜色正确。


例如,最后一组数据的图为如下(黄色为黑底)



这题做法要比较巧的方法,否则会超时,我的复杂度为O(n+5*k*k),也不知道各位大佬有没有更加快的方法


1

我们可以发现一个完整的黑白相间的图形是由2k为边长的正方形组成的

所以我把所有的点先对他们的x%2k ,y%2k处理一下 ,分不同颜色标记在w[][]和b[][]

这里所花费时间为n

然后dp,w[i][j]代表i*j这个长方形中有多少个白色的点 

             b[i][j]代表i*j这个长方形中有多少个黑色的点

这里所花费时间为4*k*k



2

我们不知道原点,那就暴力所有可能的情况,你会发现只需要沿x暴力k次,沿y暴力k次,然后我们会发现如下情况(假设k=4)



这是最优状况(黄色不一定代表是黑色的,也有可以是白色的)



这是一般情况

所以可以说s1=五块黑色中所存在的点(可以利用b[][]算出)+四块白色中所存在的点

(反着来) s2=五块白色中所存在的点(可以利用w[][]算出)+四块黑色中所存在的点


不存在的块点数为0


s=Max(s1,s2)


这里所花费时间为k*k


乎,写的时候比较小心,一次AC

#include <cstdio>
#include <algorithm>
using namespace std;
int w[2005][2005],b[2005][2005];
int jisuan(int x1,int y1,int x2,int y2,int t[][2005]){
    if(x1>x2||y1>y2)
        return 0;
    return t[x2][y2]-t[x1-1][y2]-t[x2][y1-1]+t[x1-1][y1-1];
}
int main() {
    int n,k,x,y;
    char z;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%d%d %c",&x,&y,&z);
        if(z=='W')
            w[x%(2*k)+1][y%(2*k)+1]++;
        else
            b[x%(2*k)+1][y%(2*k)+1]++;
    }
    for(int i=1;i<=2*k;i++)
        for (int j=1;j<=2*k ;j++) {
            w[i][j]=w[i-1][j]-w[i-1][j-1]+w[i][j-1]+w[i][j];
            b[i][j]=b[i-1][j]-b[i-1][j-1]+b[i][j-1]+b[i][j];
        }
    int mmax=0;
    int t1,t2;
    for( x=0;x<k;x++)
        for(y=0;y<k;y++)
        {
            t1=jisuan(x+1,y+1,x+k, y+k,w)+jisuan(1,1,x,y,w)+jisuan(x+k+1, y+k+1,2*k,2*k,w)+jisuan(x+k+1, 1,2*k,y,w)+jisuan(1, k+y+1,x,2*k,w);//百色最多五种
            t1+=jisuan(1, y+1, x, y+k, b)+jisuan(x+1,1,x+k, y, b)+jisuan(x+k+1, y+1, 2*k, y+k, b)+jisuan(x+1, y+k+1, x+k, 2*k, b);//黑色有4种
            
            //反着来一下
            t2=jisuan(x+1,y+1,x+k, y+k,b)+jisuan(1,1,x,y,b)+jisuan(x+k+1, y+k+1,2*k,2*k,b)+jisuan(x+k+1, 1,2*k,y,b)+jisuan(1, k+y+1,x,2*k,b);
            t2+=jisuan(1, y+1, x, y+k, w)+jisuan(x+1,1,x+k, y, w)+jisuan(x+k+1, y+1, 2*k, y+k, w)+jisuan(x+1, y+k+1, x+k, 2*k, w);
            
            mmax=max(t1, max(t2, mmax));
        }
    printf("%d\n",mmax);
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325524691&siteId=291194637