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[URLD][x y y+x y-x][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 = 'URLD'.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 down.keys():
        if c in up.keys():
            for vd in sorted({2 * v
                              for v in up[c]} | {2 * v + 1
                                                 for v in down[c]}):
                v, d = divmod(vd, 2)
                if d == 1:
                    ans += [v - lower]
                else:
                    lower = v
    return min(ans)


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

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[URLD][x y y+x y-x][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 = 'URLD'.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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    ans += [v - lower]
                else:
                    lower = v
    return min(ans)


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

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[URLD][x y y+x y-x][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 = 'URLD'.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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    ans += [v - lower]
                else:
                    lower = v
    return min(ans)


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

print(ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 = calc(cnt[0][0], cnt[1][0])
#ans = calc(cnt[3][1], cnt[2][1])
#ans = calc(cnt[0][2], cnt[3][2])
ans = calc(cnt[1][2], cnt[2][2])
#ans = calc(cnt[0][3], cnt[2][3])
#ans = calc(cnt[3][3], cnt[1][3])

print(ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 = calc(cnt[0][0], cnt[1][0])
#ans = calc(cnt[3][1], cnt[2][1])
#ans = calc(cnt[0][2], cnt[3][2])
#ans = calc(cnt[2][2], cnt[1][2])
#ans = calc(cnt[0][3], cnt[2][3])
ans = calc(cnt[3][3], cnt[1][3])

print(ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 = calc(cnt[0][0], cnt[1][0])
#ans = calc(cnt[3][1], cnt[2][1])
#ans = calc(cnt[0][2], cnt[3][2])
#ans = calc(cnt[2][2], cnt[1][2])
ans = calc(cnt[0][3], cnt[2][3])
#ans = calc(cnt[3][3], cnt[1][3])

print(ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 = calc(cnt[0][0], cnt[1][0])
#ans = calc(cnt[3][1], cnt[2][1])
#ans = calc(cnt[0][2], cnt[3][2])
ans = calc(cnt[2][2], cnt[1][2])
#ans = calc(cnt[0][3], cnt[2][3])
#ans = calc(cnt[3][3], cnt[1][3])

print(ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 = calc(cnt[0][0], cnt[1][0])
#ans = calc(cnt[3][1], cnt[2][1])
ans = calc(cnt[0][2], cnt[3][2])
#ans = calc(cnt[2][2], cnt[1][2])
#ans = calc(cnt[0][3], cnt[2][3])
#ans = calc(cnt[3][3], cnt[1][3])

print(ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 = calc(cnt[0][0], cnt[1][0])
ans = calc(cnt[3][1], cnt[2][1])
#ans = calc(cnt[0][2], cnt[3][2])
#ans = calc(cnt[2][2], cnt[1][2])
#ans = calc(cnt[0][3], cnt[2][3])
#ans = calc(cnt[3][3], cnt[1][3])

print(ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 = calc(cnt[0][0], cnt[1][0])
#ans = calc(cnt[3][1], cnt[2][1])
#ans = calc(cnt[0][2], cnt[3][2])
#ans = calc(cnt[2][2], cnt[1][2])
#ans = calc(cnt[0][3], cnt[2][3])
#ans = calc(cnt[3][3], cnt[1][3])

print(ans * 5)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 = calc(cnt[0][0], cnt[1][0])
#ans = calc(cnt[3][1], cnt[2][1])
ans = calc(cnt[0][2], cnt[3][2])
#ans = calc(cnt[2][2], cnt[1][2])
#ans = calc(cnt[0][3], cnt[2][3])
#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 y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 = calc(cnt[0][0], cnt[1][0])
ans = calc(cnt[3][1], cnt[2][1])
#ans = calc(cnt[0][2], cnt[3][2])
#ans = calc(cnt[2][2], cnt[1][2])
#ans = calc(cnt[0][3], cnt[2][3])
#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 y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 =calc(cnt[0][0], cnt[1][0])

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

Submission status: Wrong Answer

Solution code:
N=int(input())
U,R,D,L=[],[],[],[]
for i in range(N):
  x,y,a=input().split()
  x,y=int(x),int(y)
  if a=='U':
    U.append((x,y))
  elif a=='R':
    R.append((x,y))
  elif a=='D':
    D.append((x,y))
  elif a=='L':
    L.append((x,y))

G={}
for x,y in U:
  if x not in G:
    G[x]=[(y,'U')]
  else:
    G[x].append((y,'U'))
for x,y in D:
  if x not in G:
    G[x]=[(y,'D')]
  else:
    G[x].append((y,'D'))  

time=10**10
for i in G.values():
  a=len(i)
  i=sorted(i)
  for j in range(a-1):
    if i[j][1]=='U' and i[j+1][1]=='D':
      t=(i[j+1][0]-i[j][0])*5
      time=min(t,time)
G={}
for x,y in R:
  if y not in G:
    G[y]=[(x,'R')]
  else:
    G[y].append((x,'R'))
for x,y in L:
  if y not in G:
    G[y]=[(x,'L')]
  else:
    G[y].append((x,'L'))  

for i in G.values():
  a=len(i)
  i=sorted(i)
  for j in range(a-1):
    if i[j][1]=='R' and i[j+1][1]=='L':
      t=(i[j+1][0]-i[j][0])*5
      time=min(t,time)
#UとR
G={}
for x,y in U:
  if x+y not in G:
    G[x+y]=[(y,'R')]
  else:
    G[x+y].append((y,'R'))
for x,y in R:
  if x+y not in G:
    G[x+y]=[(y,'U')]
  else:
    G[x+y].append((y,'U'))
for i in G.values():
  a=len(i)
  i=sorted(i) 
  for j in range(a-1):
    if i[j][1]=='R' and i[j+1][1]=='U':
      t=(i[j+1][0]-i[j][0])*10
      time=min(t,time)
#DとR
G={}
for x,y in D:
  if x-y not in G:
    G[x-y]=[(y,'D')]
  else:
    G[x-y].append((y,'D'))
for x,y in R:
  if x-y not in G:
    G[x-y]=[(y,'R')]
  else:
    G[x-y].append((y,'R'))
for i in G.values():
  a=len(i)
  i=sorted(i) 
  for j in range(a-1):
    if i[j][1]=='R' and i[j+1][1]=='D':
      t=(i[j+1][0]-i[j][0])*10
      time=min(t,time)
#DとL
G={}
for x,y in D:
  if x+y not in G:
    G[x+y]=[(y,'D')]
  else:
    G[x+y].append((y,'D'))
for x,y in L:
  if x+y not in G:
    G[x+y]=[(y,'L')]
  else:
    G[x+y].append((y,'L'))
for i in G.values():
  a=len(i)
  i=sorted(i) 
  for j in range(a-1):
    if i[j][1]=='L' and i[j+1][1]=='D':
      t=(i[j+1][0]-i[j][0])*10
      time=min(t,time)
#最後UL
G={}
for x,y in U:
  if x-y not in G:
    G[x-y]=[(y,'U')]
  else:
    G[x-y].append((y,'D'))
for x,y in L:
  if x-y not in G:
    G[x-y]=[(y,'L')]
  else:
    G[x-y].append((y,'L'))
for i in G.values():
  a=len(i)
  i=sorted(i) 
  for j in range(a-1):
    if i[j][1]=='U' and i[j+1][1]=='L':
      t=(i[j+1][0]-i[j][0])*10
      time=min(t,time)
if time==10**10:
  print('SAFE')
else:
  print(time)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
N = int(input())
#cnt[UDLR][x y y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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]))

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 y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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]))

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 y+x y-x][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 down.keys():
        if c in up.keys():
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 y+x y-x][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 down:
        if c in up:
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 1:
                    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 y+x y-x][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 down:
        if c in up:
            vd = sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]})
            for v, d in vd:
                if d == 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 y+x y-x][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 down:
        if c in up:
            vd = sorted([(v, 0) for v in up[c]] + [(v, 1) for v in down[c]])
            print(vd)
            for v, d in vd:
                if d == 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
import bisect
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):
    ans = [INF]
    for c in up:
        if c in down:
            ua = [-INF] + sorted(up[c])
            ans += [min(d - ua[bisect.bisect(ua, d) - 1] for d in down)]
    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)がy+x(2)が一緒:(y-x)のr-u(>=0)の最小値
#L(2)とD(1)がy+x(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([(v, 0) for v in up[c]] + [(v, 1) for v in down[c]])
            for v, d in vd:
                if d == 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)