-
Notifications
You must be signed in to change notification settings - Fork 15
/
board-meeting.py
63 lines (55 loc) · 1.76 KB
/
board-meeting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright (c) 2019 kamyu. All rights reserved.
#
# Google Code Jam 2019 World Finals - Problem A. Board Meeting
# https://codingcompetitions.withgoogle.com/codejam/round/0000000000051708/000000000016c77c
#
# Time: O(NlogM)
# Space: O(N)
#
from sys import stdout
def print_line(s):
print s
stdout.flush()
def read_line():
s = raw_input()
if s == "ERROR":
exit()
return s
def query_diff(u, v):
assert(u%2 == v%2)
print_line("%d %d" % ((u+v)//2, (u-v)//2))
return int(read_line())*2 # diff_in_U_V = 2 * diff_in_X_Y
def query_U(N, u, swap=False):
v = 2*M + u%2
if swap:
u, v = v, u
return query_diff(u, v) - N*(u%2) # minus extra diff due to adjustment
def board_meeting():
N = (query_diff(2*M+1, 2*M+1) - query_diff(2*M, 2*M)) // 2
U, V = [], [] # u = x + y, v = x - y
for swap, axis in enumerate([U, V]):
for i in xrange(N):
left, right = -2*M, 2*M
while left <= right:
mid = left + (right-left)//2
# find the first mid s.t. its diff is greater than the expected one
if query_U(N, mid+1, swap) - query_U(N, mid, swap) > -N + i*2:
right = mid-1
else:
left = mid+1
axis.append(left)
print_line("READY")
while True:
s = read_line()
if s == "DONE":
break
x, y = map(int, s.strip().split())
result = 0
for i in xrange(N):
result += abs((x+y) - U[i]) + abs((x-y) - V[i])
result //= 2 # diff_in_X_Y = diff_in_U_V / 2
print_line(result)
T, MAX_N, M, R = map(int, raw_input().strip().split())
assert(2 + 2 * MAX_N * M.bit_length() * 2 <= R)
for case in xrange(T):
board_meeting()