Problem:


Score: 600 points

Problem Statement

M-kun is a brilliant air traffic controller.

On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude.

Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows:

if U_i is U, it flies in the positive y direction;

if U_i is R, it flies in the positive x direction;

if U_i is D, it flies in the negative y direction;

if U_i is L, it flies in the negative x direction.

To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now.

If there is such a pair, find the number of seconds after which the first collision will happen.

We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously.

Constraints

1 \leq N \leq 200000

0 \leq X_i, Y_i \leq 200000

U_i is U, R, D, or L.

The current positions of the N airplanes, (X_i, Y_i), are all distinct.

N, X_i, and Y_i are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N

Output

If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen.

If there is no such pair, print SAFE.

Sample Input 1

2
11 1 U
11 47 D

Sample Output 1

230

If the airplanes keep flying as they are now, two airplanes numbered 1 and 2 will reach the coordinates (11, 24) simultaneously and collide.  

Sample Input 2

4
20 30 U
30 20 R
20 10 D
10 20 L

Sample Output 2

SAFE

No pair of airplanes will collide.

Sample Input 3

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L

Sample Output 3

100



Sample input:
2
11 1 U
11 47 D


Sample output:
230

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = 10**10


def calc(up, down):
    lower = -INF
    ans = [INF]
    for c in up:
        if c in down:
            vd = sorted([2 * v for v in up[c]] + [2 * v + 1 for v in down[c]])
            for v in vd:
                v, f = divmod(v, 2)
                if f == 1 and lower > -INF:
                    ans += [v - lower]
                    lower = -INF
                else:
                    lower = v
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans > 10**7 else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = 10**10


def calc(up, down):
    lower = -INF
    ans = [INF]
    for c in up:
        if c in down:
            vd = sorted([2 * v for v in up[c]] + [2 * v + 1 for v in down[c]])
            for v in vd:
                v, f = divmod(v, 2)
                if f == 0 and lower > -INF:
                    ans += [v - lower]
                    lower = -INF
                else:
                    lower = v
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans > 10**7 else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = 10**10


def calc(up, down):
    lower = -INF
    ans = [INF]
    for c in up:
        if c in down:
            vd = sorted([2 * v for v in up[c]] + [2 * v + 1 for v in down[c]])
            for v in vd:
                v, f = divmod(v, 2)
                if f and lower > -INF:
                    ans += [v - lower]
                    lower = -INF
                else:
                    lower = v
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans > 10**7 else ans * 5)

Submission status: Wrong Answer

Solution code:
print('SAFE')

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    lower = -INF
    ans = [INF]
    n = len(up) + len(down)
    for c in up:
        if c in down:
            vd = sorted([2 * v for v in up[c]] + [2 * v + 1 for v in down[c]])
            for v in vd:
                v, f = divmod(v, 2)
                if f and lower > -INF:
                    ans += [v - lower]
                    lower = -INF
                else:
                    lower = v
    if n != len(up) + len(down):
        raise
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    lower = -INF
    ans = [INF]
    for c in up:
        if c in down:
            vd = sorted([2 * v for v in up[c]] + [2 * v + 1 for v in down[c]])
            for v in vd:
                v, f = divmod(v, 2)
                if f and lower > -INF:
                    ans += [v - lower]
                    lower = -INF
                else:
                    lower = v
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    lower = INF
    ans = [INF]
    for c in up:
        if c in down:
            vd = sorted([2 * v for v in up[c]] + [2 * v + 1 for v in down[c]])
            for v in vd:
                v, f = divmod(v, 2)
                if f and lower != INF:
                    ans += [v - lower]
                    lower = INF
                else:
                    lower = v
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[{} for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x] = cnt[i][0].get(x, set())
    cnt[i][1][y] = cnt[i][1].get(y, set())
    cnt[i][2][p] = cnt[i][2].get(p, set())
    cnt[i][3][q] = cnt[i][3].get(q, set())
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    lower = INF
    ans = [INF]
    for c in up:
        if c in down:
            vd = sorted([2 * v for v in up[c]] + [2 * v + 1 for v in down[c]])
            for v in vd:
                v, f = divmod(v, 2)
                if f and lower != INF:
                    ans += [v - lower]
                else:
                    lower = v
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    lower = INF
    ans = [INF]
    for c in up:
        if c in down:
            vd = sorted([2 * v for v in up[c]] + [2 * v + 1 for v in down[c]])
            for v in vd:
                v, f = divmod(v, 2)
                if f and lower != INF:
                    ans += [v - lower]
                else:
                    lower = v
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    lower = INF
    ans = [INF]
    for c in up:
        if c in down:
            vd = sorted([(v, 0) for v in up[c]] + [(v, 1) for v in down[c]])
            for v, d in vd:
                if d and lower != INF:
                    ans += [v - lower]
                else:
                    lower = v
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    lower = -INF
    ans = [INF]
    for c in up:
        if c in down:
            vd = sorted([(v, 0) for v in up[c]] + [(v, 1) for v in down[c]])
            for v, d in vd:
                if d:
                    ans += [v - lower]
                else:
                    lower = v
    return min(ans)


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    lower = -INF
    ans = INF
    for c in up:
        if c in down:
            vd = sorted([(v, 0) for v in up[c]] + [(v, 1) for v in down[c]])
            for v, d in vd:
                if d:
                    ans = min(ans, v - lower)
                else:
                    lower = v
    return ans


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    i = 'UDLR'.index(u)
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    cand = up.keys() & down.keys()
    lower = -INF
    ans = INF
    for c in cand:
        for v, d in sorted([(v, 0) for v in up[c]] + [(v, 1)
                                                      for v in down[c]]):
            if d:
                ans = min(ans, v - lower)
            else:
                lower = v
    return ans


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = min(
    calc(cnt[0][0], cnt[1][0]), calc(cnt[3][1], cnt[2][1]),
    calc(cnt[0][2], cnt[3][2]), calc(cnt[2][2], cnt[1][2]),
    calc(cnt[0][3], cnt[2][3]), calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    if u == 'U':
        i = 0
    elif u == 'D':
        i = 1
    elif u == 'L':
        i = 2
    else:
        i = 3
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    cand = up.keys() & down.keys()
    lower = -INF
    ans = INF
    for c in cand:
        for v, d in sorted([(v, 0) for v in up[c]] + [(v, 1)
                                                      for v in down[c]]):
            if d:
                ans = min(ans, v - lower)
            else:
                lower = v
    return ans


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がy-x(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がy-x(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = calc(cnt[0][0], cnt[1][0])
ans = min(ans, calc(cnt[3][1], cnt[2][1]))
ans = min(ans, calc(cnt[0][2], cnt[3][2]))
ans = min(ans, calc(cnt[2][2], cnt[1][2]))
ans = min(ans, calc(cnt[0][3], cnt[2][3]))
ans = min(ans, calc(cnt[3][3], cnt[1][3]))

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    if u == 'U':
        i = 0
    elif u == 'D':
        i = 1
    elif u == 'L':
        i = 2
    else:
        i = 3
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    cand = up.keys() & down.keys()
    lower = -INF
    ans = INF
    for c in cand:
        for v, d in sorted([(v, 0) for v in up[c]] + [(v, 1)
                                                      for v in down[c]]):
            if d:
                ans = min(ans, v - lower)
            else:
                lower = v
    return ans


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がx-y(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がx-y(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = calc(cnt[0][0], cnt[1][0])
ans = min([ans] + [calc(cnt[3][1], cnt[2][1])])
ans = min([ans] + [calc(cnt[0][2], cnt[3][2])])
ans = min([ans] + [calc(cnt[2][2], cnt[1][2])])
ans = min([ans] + [calc(cnt[0][3], cnt[2][3])])
ans = min([ans] + [calc(cnt[3][3], cnt[1][3])])

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y x+y x-y][value]=set()
cnt = [[collections.defaultdict(set) for _ in range(4)] for _ in range(4)]
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    p = y + x
    q = y - x
    if u == 'U':
        i = 0
    elif u == 'D':
        i = 1
    elif u == 'L':
        i = 2
    else:
        i = 3
    cnt[i][0][x].add(y)
    cnt[i][1][y].add(x)
    cnt[i][2][p].add(q)
    cnt[i][3][q].add(p)

INF = float('inf')


def calc(up, down):
    cand = up.keys() & down.keys()
    lower = -INF
    ans = INF
    for c in cand:
        for v, d in sorted([(v, 0) for v in up[c]] + [(v, 1)
                                                      for v in down[c]]):
            if d:
                ans = min(ans, v - lower)
            else:
                lower = v
            print(v, d, lower, ans)
    return ans


#U(0)とD(1)がx(0)が一緒:d-u(>=0)の最小値
#R(3)とL(2)がy(1)が一緒:l-r(>=0)の最小値
#U(0)とR(3)がx+y(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がx+y(2)が一緒:(y-x)のd-l(>=0)の最小値
#U(0)とL(2)がx-y(3)が一緒:(y+x)のl-u(>=0)の最小値
#R(3)とD(1)がx-y(3)が一緒:(y+x)のd-r(>=0)の最小値
ans = calc(cnt[0][0], cnt[1][0])
ans = min([ans] + [calc(cnt[3][1], cnt[2][1])])
ans = min([ans] + [calc(cnt[0][2], cnt[3][2])])
ans = min([ans] + [calc(cnt[2][2], cnt[1][2])])
ans = min([ans] + [calc(cnt[0][3], cnt[2][3])])
ans = min([ans] + [calc(cnt[3][3], cnt[1][3])])

print('SAFE' if ans == INF else ans * 5)

Submission status: Wrong Answer

Solution code:
N = int(input())
plane = [0] * N
plane2 = [0] * N
plane3 = [0] * N
houkou = ["U", "D", "R", "L"]
for i in range(N):
  x, y, p = list(map(str, input().split()))
  x = int(x)
  y = int(y)
  p2 = houkou.index(p)
  plane[i] = [x, y, x + y, x - y, p2]
  plane2[i] = [x, x - y, y, p2]
  plane3[i] = [y, x - y, x, p2]
#x座標が同じでぶつかる
plane2 = sorted(plane2, key=lambda x:x[0])
print(plane2)
time = 10 ** 10
for k in range(1, N):
    one = plane2[k] 
    two = plane2[k - 1]  
    if one[0] == two[0]:
      if one[2] > two[2]:
        if (one[3] == 1) and (two[3] == 0):
          time2 = (one[2] - two[2]) / 0.2
          time = min(time, time2)
      else:
        if (one[3] == 0) and (two[3] == 1):
          time2 = (two[2] - one[2]) / 0.2
          time = min(time, time2)

#y座標が同じでぶつかる
plane3 = sorted(plane3, key=lambda x:x[0])
for k in range(1, N):
    one = plane3[k]
    two = plane3[k - 1]  
    if one[0] == two[0]:
      if one[2] > two[2]:
        if (one[3] == 3) and (two[3] == 2):
          time2 = (one[2] - two[2]) / 0.2
          time = min(time, time2)
      else:
        if (one[3] == 2) and (two[3] == 3):
          time2 = (two[2] - one[2]) / 0.2
          time = min(time, time2)
      

def go(x, y, u, t):
  if u == 0:
    return x, int(y + t * 0.1)
  elif u== 1:
    return x, int(y - t * 0.1)
  elif u == 2:
    return int(x + t * 0.1), y
  else:
    return int(x - t * 0.1), y
  
          
#和が同じでぶつかる
plane = sorted(plane, key=lambda x:x[2])
for k in range(1, N):
    one = plane[k]
    two = plane[k - 1]
                
    if plane[k][2] == plane[k - 1][2]:
      time2 = abs(one[0] - two[0]) / 0.1
      max_x = max(one[0],two[0])
      min_x = min(one[0], two[0])
      max_y = max(one[1], two[1])
      min_y = min(one[1], two[1])
      L = [[max_x, max_y], [min_x, min_y]]
      #k
      k_x, k_y = go(one[0], one[1], one[4], time2)
      #k - 1
      k2_x, k2_y = go(two[0], two[1], two[4], time2)
      if [k_x, k_y] in L:
        if [k2_x, k2_y] in L:
          if [k_x, k_y] == [k2_x, k2_y]:
            time = min(time, time2)
          
#差が同じでぶつかる
plane = sorted(plane, key=lambda x:x[3])
#print(plane)
for k in range(1, N):
    one = plane[k]
    two = plane[k - 1]
                
    if plane[k][3] == plane[k - 1][3]:
      time2 = abs(one[0] - two[0]) / 0.1
      max_x = max(one[0],two[0])
      min_x = min(one[0], two[0])
      max_y = max(one[1], two[1])
      min_y = min(one[1], two[1])
      L = [[max_x, min_y], [min_x, max_y]]
      #k
      k_x, k_y = go(one[0], one[1], one[4], time2)
      #k - 1
      k2_x, k2_y = go(two[0], two[1], two[4], time2)
      if [k_x, k_y] in L:
        if [k2_x, k2_y] in L:
          if [k_x, k_y] == [k2_x, k2_y]:
            time = min(time, time2)          
            
if time == 10 ** 10:
  print("SAFE")
else:
  print(int(time))
#print(plane)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n, = map(int, readline().split())

M = 2*10**5+2
Uy = [[] for _ in range(M)]
Dy = [[] for _ in range(M)]

Rx = [[] for _ in range(M)]
Lx = [[] for _ in range(M)]

d1U = {} # 右上、x+y UR
d1R = {} # 右上、x+y UR
d2U = {} # 左上、x-y UL
d2L = {} # 左上、x-y UL
d3L = {} # 左下、x+y LD
d3D = {} # 左下、x+y LD
d4D = {} # 右下、x-y DR
d4R = {} # 右下、x-y DR

for _ in range(n):
    x,y,z = readline().split()
    x = int(x); y = int(y)
    if z=="U":
        Uy[x].append(y)
        dp,dn = d1U,d2U
    elif z=="D":
        Dy[x].append(y)
        dp,dn = d3D,d4D
    elif z=="R":
        Rx[y].append(x)
        dp,dn = d1R,d4R
    elif z=="L":
        Lx[y].append(x)
        dp,dn = d3L,d2L
    
    v = x+y
    if v in dp: dp[v].append(x)
    else: dp[v] = [x]
    v = x-y
    if v in dn: dn[v].append(x)
    else: dn[v] = [x]

def get(a,b):
    v = INF
    lb = len(b)
    j = 0
    for ai in a:
        while j < lb and b[j] < ai:
            j += 1
        if j < lb:
            v = min(v,b[j]-ai)
    return v

def syoumen(lst1,lst2):
    v = INF
    for i in range(M):
        if lst1[i] and lst2[i]:
            v = min(v,get(lst1[i],lst2[i]))
    return v*5

def naname(d1,d2):
    v = INF
    for i in range(-2*M,2*M):
        if i in d1 and i in d2:
            v = min(v,get(d1[i],d2[i]))
    return v*10

INF = 1<<30
ans = INF

ans = min(ans,syoumen(Uy,Dy))
ans = min(ans,syoumen(Rx,Lx))

ans = min(ans,naname(d1U,d1R))
ans = min(ans,naname(d2U,d2L))
ans = min(ans,naname(d3D,d3L))
ans = min(ans,naname(d4R,d4D))

if ans == INF:
    print("SAFE")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
import bisect
n=int(input())
arr=[list(input().split()) for _ in range(n)]
yaxis={}
xaxis={}
s1axis={}
s2axis={}
t1axis={}
t2axis={}

t=list()

for i in range(n):
    x,y,u=arr[i]
    x=int(x)
    y=int(y)
    if u=="U" or u=="R":
        if x+y not in s1axis:
            s1axis[x+y]=[[y,u]]
        else:
            s1axis[x+y].append([y,u])
    if u=="D" or u=="L":
        if x+y not in s2axis:
            s2axis[x+y]=[[y,u]]
        else:
            s2axis[x+y].append([y,u])
    if u=="U" or u=="L":
        if y-x not in t1axis:
            t1axis[y-x]=[[y,u]]
        else:
            t1axis[y-x].append([y,u])
    if u=="D" or u=="R":
        if y-x not in t2axis:
            t2axis[y-x]=[[y,u]]
        else:
            t2axis[y-x].append([y,u])
    if u=="U" or u=="D":
        if x in xaxis:
            xaxis[x].append([y,u])
        else:
            xaxis[x]=[[y,u]]
    else:
        if y in yaxis:
            yaxis[y].append([x,u])
        else:
            yaxis[y]=[[x,u]]

for s in xaxis:
    s=sorted(xaxis[s],key=lambda x: x[0])
    for k in range(1,len(s)):
        if s[k-1][1]=="U" and s[k][1]=="D":
            time=(s[k][0]-s[k-1][0])*5
            heapq.heappush(t,time)
for s in yaxis:
    s=sorted(yaxis[s],key=lambda x: x[0])
    for k in range(1,len(s)):
        if s[k-1][1]=="R" and s[k][1]=="L":
            time=(s[k][0]-s[k-1][0])*5
            heapq.heappush(t,time)
for s in s1axis:
    s=sorted(s1axis[s],key=lambda x: x[0])
    for k in range(1,len(s)):
        if s[k-1][1]=="U" and (s[k][1]=="R" or s[k][1]=="L"):
            time=10*(s[k][0]-s[k-1][0])
            heapq.heappush(t,time)

for s in s2axis:
    s=sorted(s2axis[s],key=lambda x: x[0])
    for k in range(1,len(s)):
        if s[k-1][1]=="L" and s[k][1]=="D":
            time=10*(s[k][0]-s[k-1][0])
            heapq.heappush(t,time)
 
for s in t1axis:
    s=sorted(t1axis[s],key=lambda x: x[0])
    for k in range(1,len(s)):
        if s[k-1][1]=="R" and s[k][1]=="D":
            time=10*(s[k][0]-s[k-1][0])
            heapq.heappush(t,time)

for s in t2axis:
    s=sorted(t2axis[s],key=lambda x: x[0])
    for k in range(1,len(s)):
        if s[k-1][1]=="U" and s[k][1]=="L":
            time=10*(s[k][0]-s[k-1][0])
            heapq.heappush(t,time)
if not t:
  print("SAFE")
else:
  print(heapq.heappop(t))

Submission status: Wrong Answer

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
import bisect
n=int(input())
arr=[list(input().split()) for _ in range(n)]
yaxis={}
xaxis={}
saxis={}
t=list()

for i in range(n):
    x,y,u=arr[i]
    x=int(x)
    y=int(y)
    if x+y not in saxis:
        saxis[x+y]=[[y,u]]
    else:
        saxis[x+y].append([y,u])
    if u=="U" or u=="D":
        if x in xaxis:
            xaxis[x].append([y,u])
        else:
            xaxis[x]=[[y,u]]
    else:
        if y in yaxis:
            yaxis[y].append([x,u])
        else:
            yaxis[y]=[[x,u]]

for s in xaxis:
    s=sorted(xaxis[s],key=lambda x: x[0])
    for k in range(1,len(s)):
        if s[k-1][1]=="U" and s[k][1]=="D":
            time=(s[k][0]-s[k-1][0])*5
            heapq.heappush(t,time)
for s in yaxis:
    s=sorted(yaxis[s],key=lambda x: x[0])
    for k in range(1,len(s)):
        if s[k-1][1]=="R" and s[k][1]=="L":
            time=(s[k][0]-s[k-1][0])*5
            heapq.heappush(t,time)
for s in saxis:
    s=sorted(saxis[s],key=lambda x: x[0])
    for k in range(1,len(s)):
        if s[k-1][1]=="U" and (s[k][1]=="R" or s[k][1]=="L"):
            time=10*(s[k][0]-s[k-1][0])
            heapq.heappush(t,time)
        elif (s[k-1][1]=="R" or s[k-1][1]=="L") and s[k][1]=="D":
            time=10*(s[k][0]-s[k-1][0])
            heapq.heappush(t,time)
if not t:
  print("SAFE")
else:
  print(heapq.heappop(t))