Checker

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:
这里写图片描述
AtCoDeer has N desires. The i-th desire is represented by xi, yi 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 ≤ 10^5
    1 ≤ K ≤ 1000
0 ≤ xi ≤ 10^9
    0 ≤ yi ≤ 10^9
    If i ≠ j, then (xi,yi) ≠ (xj,yj).
    ci is B or W.
    N, K, xi 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

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

Sample Output 1

4

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

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4

思路:
首先题中点的坐标范围太广,我们肯定直接开数组肯定是不现实的,遇到这种情况我一般是把数据离散化不过这里我们可以利用图中的规律性进行点收缩——我们可以发现任意一个点的x或y坐标加减2*K之后的点的颜色属性是相同的。也就是说无论图怎么改这些点的颜色是相同的,那么任意一个点我们就可以在(2*K,2*K)内找到一个点代替。这样我们就可以把所有点都收缩到(2*K,2*K)里了。同样任意一个点的x或y坐标加减K之后的点的颜色属性是相反的,也就是说如果x或y坐标加减K之后的点为黑那么这个点就必定为白,那么这个点成立就可以转化加减K之后的那个点成立,即反色。这样就可以把所有点都变成同样的颜色。
这里写图片描述
在(2*K,2*K)内黑白分布如图,那么我们找到黑色区域内有多少黑点(换成白点也一样)即是有多少条成立,直接枚举肯定不可能所以我们可以用二维前缀和。

代码:

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int N,K;

int board[2005][2005];

int getBlackNum(int x1,int y1,int x2,int y2)
//计算以x1,y1为左上角x2,y2为右下角的区域内黑点的数量 
{
    return board[x2][y2] - board[x1][y2] - board[x2][y1] + board[x1][y1];
}

int main()
{
    scanf("%d %d",&N,&K);
    char ch;
    int x,y;
    for(int i=0 ; i<N ; i++)
    {
        scanf("%d %d %c",&x,&y,&ch);
        if(ch == 'W')x += K;//点的反色 
        x %= K*2;//这里通过%操作把点收缩到2K*2K内。 
        y %= K*2;
        board[x+1][y+1]++;//从(1,1)开始
    }
    for(int i=1 ; i<=2*K ; i++)
    {//二维前缀和 
        for(int j=1 ; j<=2*K ; j++)
        {
            board[i][j] += board[i-1][j] + board[i][j-1] - board[i-1][j-1];
            //容斥定理
        }
    }
    int Max = 0;
    for(int i=1; i<=K ; i++)
    {
        for(int j=1 ; j<=K ; j++)
        {
            int mid = getBlackNum(0,0,i,j) + getBlackNum(0,j+K,i,2*K) 
                    + getBlackNum(i+K,0,2*K,j) + getBlackNum(i+K,j+K,2*K,2*K)
                    + getBlackNum(i,j,i+K,j+K);
            Max = max(Max,max(mid,N-mid));
        }
    }
    printf("%d\n",Max);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值