Problem Description
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 ≤ 105
- 1 ≤ K ≤ 1000
- 0 ≤ xi ≤ 109
- 0 ≤ yi ≤ 109
- If i ≠ j, then (xi,yi) ≠ (xj,yj).
- ci is
B
orW
.- 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 cNOutput
Print the maximum number of desires that can be satisfied at the same time.
Example
Sample Input 1
4 3
0 1 W
1 2 W
5 3 B
5 4 BSample 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 WSample 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 WSample Output 3
4
题意:有一个无边际的二维网格,网格中交错存在着一些 k*k 的黑白方块,现在有 n 个询问,每次询问是 x y c,代表坐标 (x,y) 的颜色是 c,其中 B 是黑色,W 是白色,问这 n 个询问有多少个是满足的
思路:
由于网格是边界的,n 也极大,无法通过建立数组来模拟所有的颜色情况,但可以注意到黑白方格是以 k*k 为大小交错出现的,那么可以将这个无边际的网格看成一个 2k*2k 的网格,然后再输入过程中,将 n 个询问通过取模压缩到这个 2k*2k 的网格中
在压缩后,直接枚举是不可取的,此时可以利用二维前缀和来进行询问判断
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int G[N*2][N*2];
int getNum(int x1,int y1,int x2,int y2){//以(x1,y1)为左上角(x2,y2)为右下角的点的数量
return G[x2][y2]-G[x1][y2]-G[x2][y1]+G[x1][y1];
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
int x,y;
char c;
scanf("%d %d %c",&x,&y,&c);
if(c=='W')//反色
x+=k;
//取模压缩
x=x%(k*2);
y=y%(k*2);
G[x+1][y+1]++;//从点(1,1)开始
}
for(int i=1;i<=2*k;i++)
for(int j=1;j<=2*k;j++)
G[i][j]+=G[i-1][j]+G[i][j-1]-G[i-1][j-1];//容斥
int res=-INF;
for(int i=1;i<=k;i++){
for(int j=1;j<=k;j++){
int temp1=getNum(0,0,i,j);
int temp2=getNum(0,j+k,i,2*k);
int temp3=getNum(i+k,0,2*k,j);
int temp4=getNum(i+k,j+k,2*k,2*k);
int temp5=getNum(i,j,i+k,j+k);
int sum=temp1+temp2+temp3+temp4+temp5;
res=max(res,max(sum,n-sum));
}
}
printf("%d\n",res);
return 0;
}