Problem:


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:

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

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

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



Sample input:
4 3
0 1 W
1 2 W
5 3 B
5 4 B


Sample output:
4

Submission status: Wrong Answer

Solution code:
import numpy as np

class Check:
	def __init__(self, k, c):
		self.k = k
		self.c = c
		self.m = np.zeros([k+1, k+1])

	def add_desire(self, x, y, c):
		cx = x + 1
		cy = y + 1

		if c == self.c:
			self.m[cx:, cy:] += 1.
			self.m[:cx, :cy] += 1.
		else:
			self.m[cx:, :cy] += 1.
			self.m[:cx, cy:] += 1.

	def get_max(self):
		return int(np.asscalar(self.m.max()))

n, k = map(int, input().split())
checkW = Check(k, "W")

for i in range(n):
	x, y, c = input().split()
	x = int(x)
	y = int(y)

	x = -1*x+k-1 if x<0 else x
	y = -1*y+k-1 if y<0 else y
	
	bigX = x//k
	bigY = y//k

	localX = x%k
	localY = y%k
	localC = c

	if ((bigX + bigY) % 2) != 0:
		localC = "W" if localC == "B" else "B"
		
	checkW.add_desire(localX, localY, localC)

print(checkW.get_max())

Submission status: Wrong Answer

Solution code:
import numpy as np

class Check:
	def __init__(self, k, c):
		self.k = k
		self.c = c
		self.m = np.zeros([k+1, k+1], np.uint32)

	def add_desire(self, x, y, c):
		cx = x + 1
		cy = y + 1

		if c == self.c:
			self.m[cx:, cy:] += 1
			self.m[:cx, :cy] += 1
		else:
			self.m[cx:, :cy] += 1
			self.m[:cx, cy:] += 1

	def get_max(self):
		return np.asscalar(self.m.max())

n, k = map(int, input().split())
checkW = Check(k, "W")

for i in range(n):
	x, y, c = input().split()
	x = int(x)
	y = int(y)

	x = -1*x+k-1 if x<0 else x
	y = -1*y+k-1 if y<0 else y
	
	bigX = x//k
	bigY = y//k

	localX = x%k
	localY = y%k
	localC = c

	if ((bigX + bigY) % 2) != 0:
		localC = "W" if localC == "B" else "B"
		
	checkW.add_desire(localX, localY, localC)

print(checkW.get_max())

Submission status: Wrong Answer

Solution code:
import numpy as np

class Check:
	def __init__(self, k, c):
		self.k = k
		self.c = c
		self.m = np.zeros([k+2, k+2], np.int32)

	def add_desire(self, x, y, c):
		cx = x + 2
		cy = y + 2

		if c == self.c:
			self.m[cx:, cy:] += 1
			self.m[:cx, :cy] += 1
		else:
			self.m[cx:, :cy] += 1
			self.m[:cx, cy:] += 1

	def get_max(self):
		return self.m.max()

n, k = map(int, input().split())
checkW = Check(k, "W")

for i in range(n):
	x, y, c = input().split()
	x = int(x)
	y = int(y)

	x = -1*x+k-1 if x<0 else x
	y = -1*y+k-1 if y<0 else y
	
	bigX = x//k
	bigY = y//k

	localX = x%k
	localY = y%k
	localC = c

	if ((bigX + bigY) % 2) != 0:
		localC = "W" if localC == "B" else "B"
		
	checkW.add_desire(localX, localY, localC)

print(checkW.get_max())

Submission status: Wrong Answer

Solution code:
import numpy as np

class Check:
	def __init__(self, k, c):
		self.k = k
		self.c = c
		self.m = np.zeros([k+1, k+1], np.int32)

	def add_desire(self, x, y, c):
		cx = x + 1
		cy = y + 1

		if c == self.c:
			self.m[cx:, cy:] += 1
			self.m[:cx, :cy] += 1
		else:
			self.m[cx:, :cy] += 1
			self.m[:cx, cy:] += 1

	def get_max(self):
		return self.m.max()

n, k = map(int, input().split())
checkW = Check(k, "W")

for i in range(n):
	x, y, c = input().split()
	x = int(x)
	y = int(y)

	x = -1*x+k-1 if x<0 else x
	y = -1*y+k-1 if y<0 else y
	
	bigX = x//k
	bigY = y//k

	localX = x%k
	localY = y%k
	localC = c

	if ((bigX + bigY) % 2) != 0:
		localC = "W" if localC == "B" else "B"
		
	checkW.add_desire(localX, localY, localC)

print(checkW.get_max())

Submission status: Wrong Answer

Solution code:
import numpy as np

class Check:
	def __init__(self, k, c):
		self.k = k
		self.c = c
		self.m = np.zeros([k, k], np.int32)

	def add_desire(self, x, y, c):
		cx = x + 1
		cy = y + 1

		if c == self.c:
			self.m[cx:, cy:] += 1
			self.m[:cx, :cy] += 1
		else:
			self.m[cx:, :cy] += 1
			self.m[:cx, cy:] += 1

	def get_max(self):
		return np.abs(self.m).max()

n, k = map(int, input().split())
checkW = Check(k, "W")

for i in range(n):
	x, y, c = input().split()
	x = int(x)
	y = int(y)

	x = -1*x+k-1 if x<0 else x
	y = -1*y+k-1 if y<0 else y
	
	bigX = x//k
	bigY = y//k

	localX = x%k
	localY = y%k
	localC = c

	if ((bigX + bigY) % 2) != 0:
		localC = "W" if localC == "B" else "B"
		
	checkW.add_desire(localX, localY, localC)

print(checkW.get_max())

Submission status: Wrong Answer

Solution code:
class Desire:
	def __init__(self, x, y, c):
		# c for color
		self.x = x
		self.y = y
		self.c = c

class Check:
	def __init__(self, k, c):
		self.k = k
		self.c = c
		self.desires = []

	def add_desire(self, desire):
		self.desires.append(desire)

	def get_max(self):
		_max = 0
		for i in range(self.k):
			for j in range(self.k):
				count = 0
				for desire in self.desires:
					if (desire.x >= i and desire.y >= j) or (desire.x < i and desire.y < j):
						count += 1 if desire.c == self.c else 0

					if (desire.x >= i and desire.y < j) or (desire.x < i and desire.y >= j):
						count += 1 if desire.c != self.c else 0

				_max = count if count > _max else _max

		return _max


n, k = map(int, input().split())

checkW = Check(k, "W")

for i in range(n):
	x, y, c = input().split()
	x = int(x)
	y = int(y)

	x = -1*x+k-1 if x<0 else x
	y = -1*y+k-1 if y<0 else y
	
	bigX = x//k
	bigY = y//k

	# bigX += 1 if x%k > 0 else 0
	# bigY += 1 if y%k > 0 else 0

	localX = x%k
	localY = y%k
	localC = c

	if (bigX + bigY) % 2 != 0:
		localC = "W" if localC == "B" else "B"
		
	desire = Desire(localX, localY, localC)
	checkW.add_desire(desire)

print(checkW.get_max())

Submission status: Wrong Answer

Solution code:
import numpy as np


def count_conds(x_tl, y_tl, xs, ys, cs, K):
    add_dict = {"B": 0, "W": K}

    count = 0
    for x, y, c in zip(xs, ys, cs):
        x = x % (2 * K)
        y = (y + add_dict[c]) % (2 * K)

        if x_tl <= x and x < x_tl + K:
            if y_tl <= y and y < y_tl + K:
                count += 1
        else:
            if y < y_tl or y_tl + K <= y:
                count += 1

    return count


if __name__ == '__main__':
    N, K = map(int, input().split())
    lines = [input().split() for _ in range(N)]
    xs, ys, cs = zip(*[[int(line[0]), int(line[1]), line[2]] for line in lines])

    # N, K = 4, 3
    # xs = [0, 1, 5, 5]
    # ys = [1, 2, 3, 4]
    # cs = ["W", "W", "B", "B"]

    # O(NK^2)
    counts = []
    count_max = 0
    for x_tl in range(2 * K):
        for y_tl in range(2 * K):
            count = count_conds(x_tl, y_tl, xs, ys, cs, K)
            count_max = max(count_max, count)
            counts.append(count)

    print(count_max)
    # print(np.array(counts).reshape(K, K))
    # def create_bound(p, K, ub):
    #     if p - K + 1 >= 0:
    #         return [(p - K + 1, p + 1)]
    #     else:
    #         return [(0, p + 1), ((p - K + 1) % ub, ub)]
    #
    #
    # for i in range(N):
    #     x, y, c = xs[i], ys[i], cs[i]
    #     # x, y, c = input().split()
    #     nx = int(x) % K2
    #     ny = (int(y) + add_dict[c]) % K2
    #     for add in [0, K]:
    #         for xlb, xub in create_bound((nx + add) % K2, K, K2):
    #             for ylb, yub in create_bound((ny + add) % K2, K, K2):
    #                 board[xlb][ylb] += 1
    #                 board[xlb][yub] -= 1
    #                 board[xub][ylb] -= 1
    #                 board[xub][yub] += 1
    #
    # board_cum = board.cumsum(axis=0).cumsum(axis=1)
    # print(board_cum.max())

Submission status: Wrong Answer

Solution code:
import numpy as np


def count_conds(x_tl, y_tl, xs, ys, cs, K):
    K2 = K * 2
    add_dict = {"B": 0, "W": K}

    count = 0
    for x, y, c in zip(xs, ys, cs):
        x = x % K2
        y = (y + add_dict[c]) % K2

        if x_tl <= x and x < x_tl + K:
            if y_tl <= y and y < y_tl + K:
                count += 1
        else:
            if y < y_tl or y_tl + K <= y:
                count += 1

    return count


if __name__ == '__main__':
    N, K = map(int, input().split())
    lines = [input().split() for _ in range(N)]
    xs, ys, cs = zip(*[[int(line[0]), int(line[1]), line[2]] for line in lines])

    # N, K = 4, 3
    # xs = [0, 1, 5, 5]
    # ys = [1, 2, 3, 4]
    # cs = ["W", "W", "B", "B"]

    # O(NK^2)
    counts = []
    count_max = 0
    for x_tl in range(K):
        for y_tl in range(K):
            count = count_conds(x_tl, y_tl, xs, ys, cs, K)
            count_max = max(count_max, count)
            counts.append(count)

    print(count_max)
    # print(np.array(counts).reshape(K, K))
    # def create_bound(p, K, ub):
    #     if p - K + 1 >= 0:
    #         return [(p - K + 1, p + 1)]
    #     else:
    #         return [(0, p + 1), ((p - K + 1) % ub, ub)]
    #
    #
    # for i in range(N):
    #     x, y, c = xs[i], ys[i], cs[i]
    #     # x, y, c = input().split()
    #     nx = int(x) % K2
    #     ny = (int(y) + add_dict[c]) % K2
    #     for add in [0, K]:
    #         for xlb, xub in create_bound((nx + add) % K2, K, K2):
    #             for ylb, yub in create_bound((ny + add) % K2, K, K2):
    #                 board[xlb][ylb] += 1
    #                 board[xlb][yub] -= 1
    #                 board[xub][ylb] -= 1
    #                 board[xub][yub] += 1
    #
    # board_cum = board.cumsum(axis=0).cumsum(axis=1)
    # print(board_cum.max())

Submission status: Wrong Answer

Solution code:
import numpy as np


def count_conds(x_tl, y_tl, xs, ys, cs, K):
    K2 = K * 2
    add_dict = {"B": 0, "W": K}

    count = 0
    for x, y, c in zip(xs, ys, cs):
        x = x % K2
        y = (y + add_dict[c]) % K2

        if x_tl <= x and x < x_tl + K:
            if y_tl <= y and y < y_tl + K:
                count += 1
        else:
            if y < y_tl or y_tl + K <= y:
                count += 1

    return count


if __name__ == '__main__':
    # N, K = map(int, input().split())
    # lines = [input().split() for _ in range(N)]
    # xs, ys, cs = zip(*[[int(line[0]), int(line[1]), line[2]] for line in lines])

    N, K = 4, 3
    xs = [0, 1, 5, 5]
    ys = [1, 2, 3, 4]
    cs = ["W", "W", "B", "B"]

    # O(NK^2)
    counts = []
    count_max = 0
    for x_tl in range(K):
        for y_tl in range(K):
            count = count_conds(x_tl, y_tl, xs, ys, cs, K)
            count_max = max(count_max, count)
            counts.append(count)

    print(count_max)
    # print(np.array(counts).reshape(K, K))
    # def create_bound(p, K, ub):
    #     if p - K + 1 >= 0:
    #         return [(p - K + 1, p + 1)]
    #     else:
    #         return [(0, p + 1), ((p - K + 1) % ub, ub)]
    #
    #
    # for i in range(N):
    #     x, y, c = xs[i], ys[i], cs[i]
    #     # x, y, c = input().split()
    #     nx = int(x) % K2
    #     ny = (int(y) + add_dict[c]) % K2
    #     for add in [0, K]:
    #         for xlb, xub in create_bound((nx + add) % K2, K, K2):
    #             for ylb, yub in create_bound((ny + add) % K2, K, K2):
    #                 board[xlb][ylb] += 1
    #                 board[xlb][yub] -= 1
    #                 board[xub][ylb] -= 1
    #                 board[xub][yub] += 1
    #
    # board_cum = board.cumsum(axis=0).cumsum(axis=1)
    # print(board_cum.max())

Submission status: Wrong Answer

Solution code:
_in_ = list(map(int,input().split()))
n,k = _in_[0],_in_[1]

dots = []
for i in range(n):
  _in_ = input().split()
  if(int(_in_[0])%(k*2)>=k):
    dots.append((int(_in_[0])%(k),(int(_in_[1])+n)%(k*2),_in_[2]))
  else:
    dots.append((int(_in_[0])%(k*2),int(_in_[1])%(k*2),_in_[2]))
  
_max_ = 0
for i in range(k):
  for j in range(2*k):
    cnt=0
    for d in dots:
      if ((d[0]>i and (d[1]<j or d[1]>=k+j)) or (d[0]<=i and d[1]>=j and d[1]<k+j)):
      	if(d[2]=='W'):
          cnt+=1
      else:
      	if(d[2]=='B'):
          cnt+=1
    if(_max_ < max(cnt ,(n-cnt))):
       _max_ = max(cnt ,(n-cnt))

print(_max_)

Submission status: Wrong Answer

Solution code:

# import time

# 入力部
N, K = map(int, input().split())
data_list = []
for i in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    data_list.append([x, y, c])

# start = time.time()

# スコア行列[K, K]
# 白チェッカー左下を(x, y)としたときの正解数-誤答数
score_matrix = [[0 for x in range(K+1)] for y in range(K+1)]

# time1 = time.time()

# 白チェッカー左下が(0, 0)のときの希望の数
base_hope = 0
for data in data_list:
    x, y, c = data[0] // K, data[1] // K, data[2]
    x_coord, y_coord = data[0] % K, data[1] % K
    # 正解
    if ((x + y) % 2 == 0 and c == "W") or ((x + y) % 2 == 1 and c == "B"):
        score_matrix[y_coord+1][x_coord+1] += 1
        base_hope += 1
    # 誤答
    else:
        score_matrix[y_coord+1][x_coord+1] -= 1

# time2 = time.time()

# スコア行列の二次元累積和を求める O(K^2)
xy = [(x, y) for x in range(1, K+1) for y in range(1, K+1)]
for x, y in xy:
    accum = score_matrix[y][x] + score_matrix[y][x-1] + score_matrix[y-1][x] - score_matrix[y-1][x-1]
    score_matrix[y][x] = accum

# time3 = time.time()

# 希望行列を累積和に基づき計算していく O(K^2)
# 同時に最大値を計算
max_hope = 0
for x, y in xy:
    diff = score_matrix[K-1][x-1] + score_matrix[y-1][K-1] - 2 * score_matrix[y-1][x-1]
    hope = base_hope - diff
    true_hope = max(hope, N - hope)
    if true_hope > max_hope:
        max_hope = true_hope

print(max_hope)

Submission status: Wrong Answer

Solution code:

# 入力
N, K = map(int, input().split())
info_list = []
for i in range(N):
    tmp = input().split()
    tmp[0], tmp[1] = int(tmp[0]), int(tmp[1])
    info_list.append(tmp)

# mod Kとして同値類に分類し正解、不正解をカウント
mod_info = [[[0, 0] for i in range(K)] for j in range(K)]
for info in info_list:
    x, y, c = info[0] // K, info[1] // K, info[2]
    col, row = info[0] % K, info[1] % K
    if ((x + y) % 2 == 0 and c == "W") or ((x + y) % 2 == 1 and c == "B"):
        mod_info[row][col][0] += 1
    else:
        mod_info[row][col][1] += 1
col_info = [[0, 0] for i in range(K)]
row_info = [[0, 0] for i in range(K)]
for x in range(K):
    for y in range(K):
        col_info[x][0] += mod_info[y][x][0]
        col_info[x][1] += mod_info[y][x][1]
        row_info[y][0] += mod_info[y][x][0]
        row_info[y][1] += mod_info[y][x][1]


# 初期値（白チェッカーの左下が(0, 0)の場合）の計算
correct, wrong = 0, 0
for x in range(K):
    for y in range(K):
        correct += mod_info[y][x][0]
        wrong += mod_info[y][x][1]

table = [[[0, 0] for i in range(K)] for j in range(K)]
table[0][0][0] = correct
table[0][0][1] = wrong

for x in range(1, K):
    table[0][x][0] = table[0][x-1][0] - col_info[x-1][0] + col_info[x-1][1]
    table[0][x][1] = table[0][x-1][1] - col_info[x-1][1] + col_info[x-1][0]
for y in range(1, K):
    table[y][0][0] = table[y-1][0][0] - row_info[y-1][0] + row_info[y-1][1]
    table[y][0][1] = table[y-1][0][1] - row_info[y-1][1] + row_info[y-1][0]
for x in range(1, K):
    for y in range(1, K):
        table[y][x][0] = table[y-1][x-1][0] - row_info[y-1][0] + row_info[y-1][1] \
                                            - col_info[x-1][0] + col_info[x-1][1] \
                                            + mod_info[y][x][0] - mod_info[y][x][1]
        table[y][x][1] = table[y-1][x-1][1] - row_info[y-1][1] + row_info[y-1][0] \
                                            - col_info[x-1][1] + col_info[x-1][0] \
                                            + mod_info[y][x][1] - mod_info[y][x][0]

# このテーブルのうち最大の要素を選択
max_count = 0
for x in range(K):
    for y in range(K):
        for c in [0, 1]:
            if table[y][x][c] > max_count:
                max_count = table[y][x][c]
print(max_count)

Submission status: Wrong Answer

Solution code:

# 入力
N, K = map(int, input().split())
info_list = []
for i in range(N):
    tmp = input().split()
    tmp[0], tmp[1] = int(tmp[0]), int(tmp[1])
    info_list.append(tmp)

# mod Kとして同値類に分類し正解、不正解をカウント
mod_info = [[[0, 0] for i in range(K)] for j in range(K)]
for info in info_list:
    x, y, c = info[0] // K, info[1] // K, info[2]
    col, row = info[0] % K, info[1] % K
    if ((x + y) % 2 == 0 and c == "W") or ((x + y) % 2 == 1 and c == "B"):
        mod_info[row][col][0] += 1
    else:
        mod_info[row][col][1] += 1
col_info = [[0, 0] for i in range(K)]
row_info = [[0, 0] for i in range(K)]
for x in range(K):
    for y in range(K):
        col_info[x][0] += mod_info[y][x][0]
        col_info[x][1] += mod_info[y][x][1]
        row_info[y][0] += mod_info[y][x][0]
        row_info[y][1] += mod_info[y][x][1]


# 初期値（白チェッカーの左下が(0, 0)の場合）の計算
correct, wrong = 0, 0
for x in range(K):
    for y in range(K):
        correct += mod_info[y][x][0]
        wrong += mod_info[y][x][1]

table = [[[0, 0] for i in range(K)] for j in range(K)]
table[0][0][0] = correct
table[0][0][1] = wrong

for x in range(1, K):
    table[0][x][0] = table[0][x-1][0] - col_info[x-1][0] + col_info[x-1][1]
    table[0][x][1] = table[0][x-1][1] - col_info[x-1][1] + col_info[x-1][0]
for y in range(1, K):
    table[y][0][0] = table[y-1][0][0] - row_info[y-1][0] + row_info[y-1][1]
    table[y][0][1] = table[y-1][0][1] - row_info[y-1][1] + row_info[y-1][0]
for x in range(1, K):
    for y in range(1, K):
        table[y][x][0] = table[y-1][x-1][0] - row_info[y-1][0] + row_info[y-1][1] \
                                            - col_info[x-1][0] + col_info[x-1][1] \
                                            + mod_info[y-1][x-1][0] - mod_info[y-1][x-1][1]
        table[y][x][1] = table[y-1][x-1][1] - row_info[y-1][1] + row_info[y-1][0] \
                                            - col_info[x-1][1] + col_info[x-1][0] \
                                            + mod_info[y-1][x-1][1] - mod_info[y-1][x-1][0]

# このテーブルのうち最大の要素を選択
max_count = 0
for x in range(K):
    for y in range(K):
        for c in [0, 1]:
            if table[y][x][c] > max_count:
                max_count = table[y][x][c]
print(max_count)

Submission status: Wrong Answer

Solution code:
import numpy as np

n,k = [int(i) for i in input().split()]


M =np.array([[0]*(2*k+1) for i in range(2*k+1)])

#2k*2kの空間に変換
for i in range(n):
    a,b,c = [i for i in input().split()]
    a = int(a)
    b = int(b)
    if c == 'W':
        M[(b+k)%(2*k)+1][a%(2*k)+1] = 1
    else:
        M[b%(2*k)+1][a%(2*k)+1] = 1


#2次元累積和
for i in range(1,2*k+1):
    for j in range(1,2*k+1):
        M[i][j] += M[i][j-1]
        
for i in range(1,2*k+1):
    for j in range(1,2*k+1):
        M[i][j] += M[i-1][j]


ans = 0    
for i in range(k+1):
    for j in range(k+1):
        s1 =M[i][j]
        s2 =M[2*k][j]-M[i+k][j]
        s3 =M[i][2*k]-M[i][j+k]
        s4 =M[k+i][k+j]-M[i+k][j]-M[i][j+k]+s1
        s5 =n-M[2*k][k+j]-M[k+i][2*k]+M[k+i][k+j]
        
        s = s1+s2+s3+s4+s5
        ans = max(ans,s)
        
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

n,k = [int(i) for i in input().split()]


M =np.array([[0]*(2*k+1) for i in range(2*k+1)])

#2k*2kの空間に変換
for i in range(n):
    a,b,c = [i for i in input().split()]
    a = int(a)
    b = int(b)
    if c == 'W':
        M[(b+k)%(2*k)+1][a%(2*k)+1] = 1
    else:
        M[b%(2*k)+1][a%(2*k)+1] = 1

print(M)
#2次元累積和
for i in range(1,2*k+1):
    for j in range(1,2*k+1):
        M[i][j] += M[i][j-1]
        
for i in range(1,2*k+1):
    for j in range(1,2*k+1):
        M[i][j] += M[i-1][j]

print(M)
ans = 0    
for i in range(k+1):
    for j in range(k+1):
        s1 =M[i][j]
        s2 =M[2*k][j]-M[i+k][j]
        s3 =M[i][2*k]-M[i][j+k]
        s4 =M[k+i][k+j]-M[i+k][j]-M[i][j+k]+s1
        s5 =n-M[2*k][k+j]-M[k+i][2*k]+M[k+i][k+j]
        
        s = s1+s2+s3+s4+s5
        ans = max(ans,s)
        
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

n,k = [int(i) for i in input().split()]

M =np.array([[0]*(2*k+1) for i in range(2*k+1)])

#2k*2kの空間に変換
for i in range(n):
    a,b,c = [i for i in input().split()]
    a = int(a)
    b = int(b)
    if c == 'w':
        M[(b+k)%(2*k)+1][a%(2*k)+1] = 1
    else:
        M[b%(2*k)+1][a%(2*k)+1] = 1

print(M)
#2次元累積和
for i in range(1,2*k+1):
    for j in range(1,2*k+1):
        M[i][j] += M[i][j-1]
        
for i in range(1,2*k+1):
    for j in range(1,2*k+1):
        M[i][j] += M[i-1][j]


ans = 0    
for i in range(1,k+1):
    for j in range(1,k+1):
        s1 =M[i][j]
        s2 =M[2*k][j]-M[i+k][j]
        s3 =M[i][2*k]-M[i][j+k]
        s4 =M[k+i][k+j]-M[i+k][j]-M[i][j+k]+s1
        s5 =n-M[2*k][k+j]-M[k+i][2*k]+M[k+i][k+j]
        
        s = s1+s2+s3+s4+s5
        ans = max(ans,s)
        
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = [ int(a) for a in input().split() ]

MOD = 2*K

desires = [ [0]*MOD for _ in range(MOD) ]

for _ in range(N):
    toks = input().split()
    x = int(toks[0])
    y = int(toks[1])
    c = toks[2]
    if c == 'B':
        desires[x%MOD][y%MOD] += 1
    else:
        desires[x%MOD][(y+K)%MOD] += 1

rows = [ [0]*MOD for _ in range(MOD) ]
squares = [ [0]*MOD for _ in range(MOD) ]

for i in range(MOD):
    rows[i][0] = sum(desires[i][0:K])

for i in range(MOD):
    for j in range(1,MOD):
        rows[i][j] = rows[i][j-1] + desires[i][j-K-1] - desires[i][j-1]

for j in range(MOD):
    squares[0][j] = sum([rows[i%MOD][j] for i in range(K) ])

for i in range(1,MOD):
    for j in range(MOD):
        squares[i][j] = squares[i-1][j] + rows[i-K-1][j] - rows[i-1][j]

best = 0
for i in range(K):
    for j in range(K):
        best = max(best, squares[i][j]+squares[i+K][j+K])
print(best)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
xyc = [list(map(str, input().split())) for i in range(N)]

score = {}

for i in range(K+1):
    for j in range(K+1):
        score[(i,j)] = 0
        for m in range(N):
            if xyc[m][2] == "B":
                if (j <= int(xyc[m][1])%(2*K) < K+j and i <= int(xyc[m][0])%(2*K) < K+i) \
                or ((j+K <= int(xyc[m][1])%(2*K) or int(xyc[m][1])%(2*K) <j)and i+K <= int(xyc[m][0])%(2*K) or int(xyc[m][0])%(2*K) < i):
                    score[(i,j)] += 1
            else:
                if (j <= int(xyc[m][1])%(2*K) < K+j and (i+K <= int(xyc[m][0])%(2*K) or int(xyc[m][0])%(2*K) < i)) \
                or ((j+K <= int(xyc[m][1])%(2*K) or int(xyc[m][1])%(2*K) < j) and i <= int(xyc[m][0])%(2*K) < i+K):
                    score[(i,j)] += 1

print(max(score.values()))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
xyc = [list(map(str, input().split())) for i in range(N)]

score = {}

for i in range(K):
    for j in range(K):
        score[(i,j)] = 0
        for m in range(N):
            if xyc[m][2] == "B":
                if (j <= int(xyc[m][1])%(2*K) < K+j and i <= int(xyc[m][0])%(2*K) < K+i) \
                or ((j+K <= int(xyc[m][1])%(2*K) or int(xyc[m][1])%(2*K) <j)and i+K <= int(xyc[m][0])%(2*K) or int(xyc[m][0])%(2*K) < i):
                    score[(i,j)] += 1
            else:
                if (j <= int(xyc[m][1])%(2*K) < K+j and (i+K <= int(xyc[m][0])%(2*K) or int(xyc[m][0])%(2*K) < i)) \
                or ((j+K <= int(xyc[m][1])%(2*K) or int(xyc[m][1])%(2*K) < j) and i <= int(xyc[m][0])%(2*K) < i+K):
                    score[(i,j)] += 1

print(max(score.values()))

Submission status: Wrong Answer

Solution code:
import math
N, K = map(int, input().split())
xyc = [list(map(str, input().split())) for i in range(N)]

score = {}

for i in range(2*K):
    for j in range(math.floor(K/2)+1):
        score[(i,j)] = 0
        for m in range(N):
            if xyc[m][2] == "B":
                if (j <= int(xyc[m][1])%(2*K) < K+j and i <= int(xyc[m][0])%(2*K) < K+i) \
                or ((j+K <= int(xyc[m][1])%(2*K) or int(xyc[m][1])%(2*K) <j)and i+K <= int(xyc[m][0])%(2*K) or int(xyc[m][0])%(2*K) < i):
                    score[(i,j)] += 1
            else:
                if (j <= int(xyc[m][1])%(2*K) < K+j and (i+K <= int(xyc[m][0])%(2*K) or int(xyc[m][0])%(2*K) < i)) \
                or ((j+K <= int(xyc[m][1])%(2*K) or int(xyc[m][1])%(2*K) < j) and i <= int(xyc[m][0])%(2*K) < i+K):
                    score[(i,j)] += 1

print(max(score.values()))

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
dream = [input().split() for line in range(N)]

max_count = 0
for i in range(K):
    for j in range(K):
        count = 0
        for d in dream:
            x = (int(d[0])+i) % (2*K)
            y = (int(d[1]) + j) % (2*K)
            c = d[2]

            pos_c = 'B'
            if (x >= K and y <K) or (x < K and y >= K):
                pos_c ='W'
            
            if c == pos_c:
                count += 1
        
        if count > max_count:
            max_count = count
        
        if max_count == K:
            break

print(max_count)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())

dic = {}
ans = 0
for i in range(N):
	x, y, c = input().split()
	x, y = map(int, (x, y))
	fixed_x, fixed_y = x % K, y % K
	if not (fixed_x, fixed_y, c) in dic:
		dic[(fixed_x, fixed_y, c)] = 1
		ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, raw_input().split())
hopes = [raw_input().split() for i in range(n)]

maxhope = 0
minhope = n

for mx in range(k):
  for my in range(k):
    ncometrue = 0
    for hope in hopes:
      x, y, c = int(hope[0]),int(hope[1]),hope[2]
      loc = (x-mx)/k + (y-my)/k
      if (c=='W' and loc%2==0) or (c=='B' and loc%2==1):
        ncometrue += 1
    maxhope = max(maxhope,ncometrue)
    minhope = min(maxhope,ncometrue)

print max(maxhope,n-minhope)

Submission status: Wrong Answer

Solution code:
N, K = (int(i) for i in input().split())
W = list()
for index in range(N):
	x, y, c = (s for s in input().split())
	x = int(x)
	y = int(y)
	c = 0 if c == "W" else 1
	if x%(2*K) != x%K:
		c = 1-c
	if y%(2*K) != y%K:
		c = 1-c
	W.append((x%K, y%K, c))

w = [[0 for _ in range(K)] for _ in range(K)]
b = [[0 for _ in range(K)] for _ in range(K)]
wx = [0 for _ in range(K)]
wy = [0 for _ in range(K)]
bx = [0 for _ in range(K)]
by = [0 for _ in range(K)]
for x, y, c in W:
	if c == 0:
		w[x][y] += 1
		wx[x] += 1
		wy[y] += 1
	else:
		b[x][y] += 1
		bx[x] += 1
		by[y] += 1

M = 0
for row in w:
	M += sum(row)

s = M
for x in range(K):
	tmp = s
	for y in range(K):
		s += by[y]
		s -= wy[y]
		M = max(s, M)
	for y in range(K):
		by[y] += w[x][y]
		by[y] -= b[x][y]
		wy[y] += b[x][y]
		wy[y] -= w[x][y]
	tmp += bx[x]
	tmp -= wx[x]
	s = tmp
	M = max(s, M)

print(M)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
    global acc, l
    if y1 >= l or x1 >= l or y2 < 0 or x2 < 0:
        return 0
    y2 = min(y2 + 1, l)
    x2 = min(x2 + 1, l)
    ret = acc[y2][x2]
    if y1 > 0:
        ret -= acc[y1][x2]
    if x1 > 0:
        ret -= acc[y2][x1]
    if y1 > 0 and x1 > 0:
        ret += acc[y1][x1]
    return  ret

def main():
    global acc, k, l
    n, k = map(int, sys.stdin.readline().split())
    xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x,
                 line.split()) for line in sys.stdin.readlines())

    l = k * 2
    yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for x, y, c in xycs)

    acc = [[0] * (l + 1) for _ in range(l + 1)]
    for y, x in yxs:
        acc[y + 1][x + 1] += 1
    for j in range(l):
        for i in range(l):
            acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]

    max_cnt = 0
    for j in range(k):
        for i in range(k):
            cnt = count_up(j, i, j + k - 1, i + k - 1) + count_up(j - k, i - k, j - 1, i - 1) + count_up(j - k, i + k, j - 1, i + l - 1) + count_up(j + k, i - k, j + l - 1, i - 1) + count_up(j + k, i + k, j + l - 1, i + l - 1)
            max_cnt = max(max_cnt, cnt)

    print(max_cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
    global acc, l
    if y1 >= l or x1 >= l or y2 < 0 or x2 < 0:
        return 0
    y1 = max(y1, 0)
    x1 = max(x1, 0)
    y2 = min(y2 + 1, l)
    x2 = min(x2 + 1, l)
    return acc[y2][x2] + acc[y1][x1] - acc[y1][x2] - acc[y2][x1]

def main():
    global acc, k, l
    n, k = map(int, sys.stdin.readline().split())
    xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x,
                 line.split()) for line in sys.stdin.readlines())

    l = k * 2
    yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for x, y, c in xycs)

    acc = [[0] * (l + 1) for _ in range(l + 1)]
    for y, x in yxs:
        acc[y + 1][x + 1] += 1
    for j in range(l):
        for i in range(l):
            acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]

    max_cnt = 0
    for j in range(k):
        for i in range(k):
            cnt = count_up(j, i, j + k - 1, i + k - 1) + count_up(j - k, i - k, j - 1, i - 1) + count_up(j - k, i + k, j - 1, i + l - 1) + count_up(j + k, i - k, j + l - 1, i - 1) + count_up(j + k, i + k, j + l - 1, i + l - 1)
            max_cnt = max(max_cnt, cnt)

    print(max_cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
    global acc, l
    if y1 > y2 or x1 > x2:
        return 0
    y2 += 1
    x2 += 1
    return acc[y2][x2] + acc[y1][x1] - acc[y1][x2] - acc[y2][x1]

def main():
    global acc, k, l
    n, k = map(int, sys.stdin.readline().split())
    xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x,
                 line.split()) for line in sys.stdin.readlines())

    l = k * 2
    yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for x, y, c in xycs)

    acc = [[0] * (l + 1) for _ in range(l + 1)]
    for y, x in yxs:
        acc[y + 1][x + 1] += 1
    for j in range(l):
        for i in range(l):
            acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]

    max_cnt = 0
    for j in range(k):
        for i in range(k):
            cnt = count_up(j, i, j + k - 1, i + k - 1) + count_up(0, 0, j - 1, i - 1) + count_up(0, i + k, j - 1, l - 1) + count_up(j + k, 0, l - 1, i - 1) + count_up(j + k, i + k, l - 1, l - 1)
            max_cnt = max(max_cnt, cnt)

    print(max_cnt)

if __name__ == '__main__':
    main()