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 sys
# from collections import Counter

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200001)]
    Y = [[] for _ in range(200001)]
    A = [[] for _ in range(200001 * 2 + 1)]
    B = [[] for _ in range(200001 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200000].append((x, u))

    # 正面衝突
    length = 1000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 5, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True


    if length == 1000000:
        print("SAFE")
    else:
        print(length)


main()

Submission status: Wrong Answer

Solution code:
def f_calc1(u1, d1, ans):
  u1.sort()
  d1.sort()

  i=0
  for x1, y1 in u1:
    while i < len(d1):
      x2, y2 = d1[i]
      if x1 < x2:
        break
      if x1 == x2:
        if y1 < y2:
          ans = min(ans, (y2-y1)*5)
          break
        i += 1
      else:
        i += 1
  return ans

def f_calc2(u1, d1, ans):
  u1.sort()
  d1.sort()

  i=0
  for x1, y1 in u1:
    while i < len(d1):
      x2, y2 = d1[i]
      if x1 < x2:
        break
      if x1 == x2:
        if y1 < y2:
          ans = min(ans, (y2-y1)*10)
          break
        i += 1
      else:
        i += 1
  return ans

n = int(input())

u1 = []
u2 = []
d1 = []
d2 = []
r1 = []
r2 = []
l1 = []
l2 = []

for _ in range(n):
  x,y,u = input().split()
  x = int(x)
  y = int(y)
  if u == 'U':
    u1.append([x, y])
    u2.append([x+y, y])
  elif u == 'D':
    d1.append([x, y])
    d2.append([x+y, x])
  elif u == 'R':
    r1.append([y, x])
    r2.append([x+y, y])
  else:
    l1.append([y, x])
    l2.append([x+y, x])
  
ans = 10**8
ans = f_calc1(u1, d1, ans)
ans = f_calc1(r1, l1, ans)
ans = f_calc2(u2, r2, ans)
ans = f_calc2(d2, l2, ans)

print(ans if ans < 10**8 else "SAFE")

Submission status: Wrong Answer

Solution code:
N = int(input())
L = {'U':[], 'R':[], 'D':[], 'L':[]}

for _ in range(N):
    x, y, u = input().split()
    L[u].append((int(x), int(y)))

ans = 10 ** 7

def solve(A):
    global ans
    A.sort()
    s = - 10 ** 6
    t = - 10 ** 6
    for p, q, r in A:
        if p != s:
            if r == 1:
                s = p
                t = q
        elif r == 1:
            t = q
        else:
            ans = (q - t) * 5

solve([(x, y, 1) for x, y in L['U']] + [(x, y, -1) for x, y in L['D']])
solve([(y, x, 1) for x, y in L['R']] + [(y, x, -1) for x, y in L['L']])
solve([(x + y, y - x, 1) for x, y in L['U']] + [(x + y, y - x, -1) for x, y in L['R']])
solve([(y - x, x + y, 1) for x, y in L['R']] + [(y - x, x + y, -1) for x, y in L['D']])
solve([(x + y, x - y, 1) for x, y in L['D']] + [(x + y, x - y, -1) for x, y in L['L']])
solve([(y - x, x + y, 1) for x, y in L['L']] + [(y - x, x + y, -1) for x, y in L['U']])

if ans == 10 ** 7:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import collections


sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    XYU = [[x for x in input().split()] for _ in range(N)]

    ans = float("inf")

    c1 = collections.defaultdict(list)
    c2 = collections.defaultdict(list)

    for x, y, u in XYU:
        x = int(x)
        y = int(y)
        if u in ("U", "D"):
            c1[x].append([y, u])
        if u in ("U", "R"):
            c2[x + y].append([y, u])

    for k in c1.keys():
        c1[k].sort()
        tmp = -float("inf")
        for y, u in c1[k]:
            if u == "U":
                tmp = y
            else:
                if tmp != -float("inf"):
                    ans = min(ans, (y - tmp) / 0.2)
                    break

    for k in c2.keys():
        c2[k].sort()
        tmp = -float("inf")
        for y, u in c2[k]:
            if u == "U":
                tmp = y
            else:
                if tmp != -float("inf"):
                    ans = min(ans, (y - tmp) / 0.1)
                    break

    XYU2 = []
    for x, y, u in XYU:
        x = int(x)
        y = int(y)
        if u == "R":
            XYU2.append([y, -x, "D"])
        elif u == "L":
            XYU2.append([y, -x, "U"])
        elif u == "D":
            XYU2.append([y, -x, "L"])
        else:
            XYU2.append([y, -x, "R"])

    c1 = collections.defaultdict(list)
    c2 = collections.defaultdict(list)
    for x, y, u in XYU2:
        x = int(x)
        y = int(y)
        if u in ("U", "D"):
            c1[x].append([y, u])
        if u in ("U", "R"):
            c2[x + y].append([y, u])

    for k in c1.keys():
        c1[k].sort()
        tmp = -float("inf")
        for y, u in c1[k]:
            if u == "U":
                tmp = y
            else:
                if tmp != -float("inf"):
                    ans = min(ans, (y - tmp) / 0.2)
                    break

    for k in c2.keys():
        c2[k].sort()
        tmp = -float("inf")
        for y, u in c2[k]:
            if u == "U":
                tmp = y
            else:
                if tmp != -float("inf"):
                    ans = min(ans, (y - tmp) / 0.1)
                    break

    XYU3 = []
    for x, y, u in XYU2:
        x = int(x)
        y = int(y)
        if u == "R":
            XYU3.append([y, -x, "D"])
        elif u == "L":
            XYU3.append([y, -x, "U"])
        elif u == "D":
            XYU3.append([y, -x, "L"])
        else:
            XYU3.append([y, -x, "R"])

    c1 = collections.defaultdict(list)
    c2 = collections.defaultdict(list)
    for x, y, u in XYU3:
        x = int(x)
        y = int(y)
        if u in ("U", "D"):
            c1[x].append([y, u])
        if u in ("U", "R"):
            c2[x + y].append([y, u])

    for k in c1.keys():
        c1[k].sort()
        tmp = -float("inf")
        for y, u in c1[k]:
            if u == "U":
                tmp = y
            else:
                if tmp != -float("inf"):
                    ans = min(ans, (y - tmp) / 0.2)
                    break

    for k in c2.keys():
        c2[k].sort()
        tmp = -float("inf")
        for y, u in c2[k]:
            if u == "U":
                tmp = y
            else:
                if tmp != -float("inf"):
                    ans = min(ans, (y - tmp) / 0.1)
                    break

    XYU4 = []
    for x, y, u in XYU3:
        x = int(x)
        y = int(y)
        if u == "R":
            XYU4.append([y, -x, "D"])
        elif u == "L":
            XYU4.append([y, -x, "U"])
        elif u == "D":
            XYU4.append([y, -x, "L"])
        else:
            XYU4.append([y, -x, "R"])

    c1 = collections.defaultdict(list)
    c2 = collections.defaultdict(list)
    for x, y, u in XYU3:
        x = int(x)
        y = int(y)
        if u in ("U", "D"):
            c1[x].append([y, u])
        if u in ("U", "R"):
            c2[x + y].append([y, u])

    for k in c1.keys():
        c1[k].sort()
        tmp = -float("inf")
        for y, u in c1[k]:
            if u == "U":
                tmp = y
            else:
                if tmp != -float("inf"):
                    ans = min(ans, (y - tmp) / 0.2)
                    break

    for k in c2.keys():
        c2[k].sort()
        tmp = -float("inf")
        for y, u in c2[k]:
            if u == "U":
                tmp = y
            else:
                if tmp != -float("inf"):
                    ans = min(ans, (y - tmp) / 0.1)
                    break

    if ans == float("inf"):
        print("SAFE")
    else:
        print(int(ans))





    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

du = {}
dd = {}
dr = {}
dl = {}
n = int(input())

p = {'U': {}, 'D': {}, 'R': {}, 'L': {}}
m = {'U': {}, 'D': {}, 'R': {}, 'L': {}}

for i in range(n):
    x, y, u = input().rstrip('\n').split()
    x = int(x)
    y = int(y)
    if x + y in p[u]:
        p[u][x+y].append(x)
    else:
        p[u][x+y] = [x]

    if x - y in m[u]:
        m[u][x-y].append(x)
    else:
        m[u][x-y] = [x]
    
    if u == 'U':
        if x in du:
            du[x].append(y)
        else:
            du[x] = [y]
    if u == 'D':
        if x in dd:
            dd[x].append(y)
        else:
            dd[x] = [y]
    if u == 'R':
        if y in dr:
            dr[y].append(x)
        else:
            dr[y] = [x]
    if u == 'L':
        if y in dl:
            dl[y].append(x)
        else:
            dl[y] = [x]

for key in p:
    for key2 in p[key]:
        if key == 'U' or key == 'R':
            p[key][key2].sort(reverse=True)
        else:
            p[key][key2].sort()

for key in m:
    for key2 in m[key]:
        if key == 'D' or key == 'R':
            m[key][key2].sort(reverse=True)
        else:
            m[key][key2].sort()

ans = 10 ** 7

for key in p['U']:
    if key not in p['R']:
        continue
    num = len(p['R'][key])
    i = 0
    for x in p['U'][key]:
        while x < p['R'][key][i]:
            if i == num - 1:
                break
            i += 1
        if x > p['R'][key][i]:
            ans = min(ans, (x - p['R'][key][i]) * 10)

for key in m['D']:
    if key not in m['R']:
        continue
    num = len(m['R'][key])
    i = 0
    for x in m['D'][key]:
        while x < m['R'][key][i]:
            if i == num - 1:
                break
            i += 1
        if x > m['R'][key][i]:
            ans = min(ans, (x - m['R'][key][i]) * 10)

for key in m['U']:
    if key not in m['L']:
        continue
    num = len(m['L'][key])
    i = 0
    for x in m['U'][key]:
        while x > m['L'][key][i]:
            if i == num - 1:
                break
            i += 1
        if x < m['L'][key][i]:
            ans = min(ans, (m['L'][key][i] - x) * 10)

for key in p['D']:
    if key not in p['L']:
        continue
    num = len(p['L'][key])
    i = 0
    for x in p['D'][key]:
        while x > p['L'][key][i]:
            if i == num - 1:
                break
            i += 1
        if x < p['L'][key][i]:
            ans = min(ans, (p['L'][key][i] - x) * 10)

for x in du:
    if x in dd:
        du[x].sort()
        dd[x].sort()
        m = len(dd[x])
        i = 0
        for yi in du[x]:
            while yi > dd[x][i]:
                if i == m - 1:
                    break
                i += 1
            if yi <= dd[x][i]:
                ans = min(ans, (dd[x][i] - yi) * 5)

for y in dr:
    if y in dl:
        dr[y].sort()
        dl[y].sort()
        m = len(dl[y])
        i = 0
        for xi in dr[y]:
            while xi > dl[y][i]:
                if i == m - 1:
                    break
                i += 1
            if xi <= dl[y][i]:
                ans = min(ans, (dl[y][i] - xi) * 5)

if ans == 10 ** 6 + 2:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

du = {}
dd = {}
dr = {}
dl = {}
n = int(input())

p = {'U': {}, 'D': {}, 'R': {}, 'L': {}}
m = {'U': {}, 'D': {}, 'R': {}, 'L': {}}

for i in range(n):
    x, y, u = input().rstrip('\n').split()
    x = int(x)
    y = int(y)
    if x + y in p[u]:
        p[u][x+y].append(x)
    else:
        p[u][x+y] = [x]

    if x - y in m[u]:
        m[u][x-y].append(x)
    else:
        m[u][x-y] = [x]
    
    if u == 'U':
        if x in du:
            du[x].append(y)
        else:
            du[x] = [y]
    if u == 'D':
        if x in dd:
            dd[x].append(y)
        else:
            dd[x] = [y]
    if u == 'R':
        if y in dr:
            dr[y].append(x)
        else:
            dr[y] = [x]
    if u == 'L':
        if y in dl:
            dl[y].append(x)
        else:
            dl[y] = [x]

for key in p:
    for key2 in p[key]:
        if key == 'U' or key == 'R':
            p[key][key2].sort(reverse=True)
        else:
            p[key][key2].sort()

for key in m:
    for key2 in m[key]:
        if key == 'D' or key == 'R':
            m[key][key2].sort(reverse=True)
        else:
            m[key][key2].sort()

ans = 10 ** 6 + 2

for key in p['U']:
    if key not in p['R']:
        continue
    num = len(p['R'][key])
    i = 0
    for x in p['U'][key]:
        while x < p['R'][key][i]:
            if i == num - 1:
                break
            i += 1
        if x > p['R'][key][i]:
            ans = min(ans, (x - p['R'][key][i]) * 10)

for key in m['D']:
    if key not in m['R']:
        continue
    num = len(m['R'][key])
    i = 0
    for x in m['D'][key]:
        while x < m['R'][key][i]:
            if i == num - 1:
                break
            i += 1
        if x > m['R'][key][i]:
            ans = min(ans, (x - m['R'][key][i]) * 10)

for key in m['U']:
    if key not in m['L']:
        continue
    num = len(m['L'][key])
    i = 0
    for x in m['U'][key]:
        while x > m['L'][key][i]:
            if i == num - 1:
                break
            i += 1
        if x < m['L'][key][i]:
            ans = min(ans, (m['L'][key][i] - x) * 10)

for key in p['D']:
    if key not in p['L']:
        continue
    num = len(p['L'][key])
    i = 0
    for x in p['D'][key]:
        while x > p['L'][key][i]:
            if i == num - 1:
                break
            i += 1
        if x < p['L'][key][i]:
            ans = min(ans, (p['L'][key][i] - x) * 10)

for x in du:
    if x in dd:
        du[x].sort()
        dd[x].sort()
        m = len(dd[x])
        i = 0
        for yi in du[x]:
            while yi > dd[x][i]:
                if i == m - 1:
                    break
                i += 1
            if yi <= dd[x][i]:
                ans = min(ans, (dd[x][i] - yi) * 5)

for y in dr:
    if y in dl:
        dr[y].sort()
        dl[y].sort()
        m = len(dl[y])
        i = 0
        for xi in dr[y]:
            while xi > dl[y][i]:
                if i == m - 1:
                    break
                i += 1
            if xi <= dl[y][i]:
                ans = min(ans, (dl[y][i] - xi) * 5)

if ans == 10 ** 6 + 2:
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
# from #15490827
import bisect
sideLen = 200000


def solve(deg1, deg2, lis, magnification):
    tmpAns = sideLen * 2
    for dx in lis[deg1]:
        if dx not in lis[deg2]:
            continue
        for dy in lis[deg1][dx]:
            t = bisect.bisect_left(lis[deg2][dx], dy)
            if t < len(lis[deg2][dx]):
                tmpAns = min(tmpAns, (lis[deg2][dx][t] - dy) * magnification)
    return tmpAns


N = int(input())
fTof = {"U": {}, "D": {}, "R": {}, "L": {}}
urld = {"U": {}, "D": {}, "R": {}, "L": {}}
ulrd = {"U": {}, "D": {}, "R": {}, "L": {}}
for _ in range(N):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == "U" or u == "D":
        if x in fTof[u]:
            fTof[u][x].append(y)
        else:
            fTof[u][x] = [y]
    else:
        if y in fTof[u]:
            fTof[u][y].append(x)
        else:
            fTof[u][y] = [x]
    if x+y in urld[u]:
        urld[u][x + y].append(y)
    else:
        urld[u][x + y] = [y]
    if x - y in ulrd[u]:
        ulrd[u][x - y].append(y)
    else:
        ulrd[u][x - y] = [y]

for i in fTof:
    for j in fTof[i]:
        fTof[i][j].sort()
for i in urld:
    for j in urld[i]:
        urld[i][j].sort()
for i in ulrd:
    for j in ulrd[i]:
        ulrd[i][j].sort()
ans = sideLen * 2
ans = min(ans, solve("R", "L", fTof, 5))
ans = min(ans, solve("U", "D", fTof, 5))
ans = min(ans, solve("U", "R", urld, 10))
ans = min(ans, solve("L", "D", urld, 10))
ans = min(ans, solve("U", "L", ulrd, 10))
ans = min(ans, solve("R", "D", ulrd, 10))
print("SAFE" if ans == sideLen * 2 else ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

sideLen = 200000


def solve_reverse():
    tmpAns = sideLen * 2
    values = []
    for num in range(N):
        if not (U[num] == "R" or U[num] == "L"):
            continue
        else:
            values.append([Y[num], X[num], U[num]])
    values.sort()
    # print(values)
    for num in range(len(values)-1):
        if not (values[num][2] == "R" and values[num + 1][2] == "L") or values[num][0] != values[num+1][0]:
            continue
        else:
            tmpAns = min(tmpAns, values[num + 1][1] - values[num][1])
            #print(1, tmpAns, values[num+1], values[num])
    return tmpAns * 5


def solve_90degree():
    tmpAns = sideLen * 2
    values = []
    for num in range(N):
        if not (U[num] == "R" or U[num] == "U"):
            continue
        else:
            values.append([X[num]+Y[num], X[num], U[num]])
    values.sort()
    for num in range(len(values)-1):
        if not (values[num][2] == "R" and values[num + 1][2] == "U") or values[num][0] != values[num+1][0]:
            continue
        else:
            tmpAns = min(tmpAns, values[num + 1][1] - values[num][1])
            #print(2, tmpAns, values[num+1], values[num])
    return tmpAns * 10


N = int(input())
X = deque()
Y = deque()
U = deque()
Lis = {"U": {}, "D": {}, "R": {}, "L": {}}

def rotate():
    degLis = ["U", "R", "D", "L", "L", "U", "R", "D"]
    for num in range(N):
        tmpX = sideLen - Y[num]
        Y[num] = X[num]
        X[num] = tmpX
        U[num] = degLis[degLis.index(U[num])+4]

for _ in range(N):
    x, y, u = map(str, input().split())
    X.append(int(x))
    Y.append(int(y))
    U.append(u)
ans = sideLen * 2

for i in range(4):
    if i >= 2:
        revNum = solve_reverse()
        ans = min(revNum, ans)
    degNum = solve_90degree()
    ans = min(degNum, ans)
    rotate()

if ans == sideLen * 2:
    print("SAFE")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

sideLen = 200000


def solve_reverse():
    tmpAns = sideLen
    values = []
    for num in range(N):
        if not (U[num] == "R" or U[num] == "L"):
            continue
        else:
            values.append([Y[num], X[num], U[num]])
    values.sort()
    # print(values)
    for num in range(len(values)-1):
        if not (values[num][2] == "R" and values[num + 1][2] == "L") or values[num][0] != values[num+1][0]:
            continue
        else:
            tmpAns = min(tmpAns, values[num + 1][1] - values[num][1])
            #print(1, tmpAns, values[num+1], values[num])
    return tmpAns * 5


def solve_90degree():
    tmpAns = sideLen
    values = []
    for num in range(N):
        if not (U[num] == "R" or U[num] == "U"):
            continue
        else:
            values.append([X[num]+Y[num], X[num], U[num]])
    values.sort()
    for num in range(len(values)-1):
        if not (values[num][2] == "R" and values[num + 1][2] == "U") or values[num][0] != values[num+1][0]:
            continue
        else:
            tmpAns = min(tmpAns, values[num + 1][1] - values[num][1])
            #print(2, tmpAns, values[num+1], values[num])
    return tmpAns * 10


N = int(input())
X = deque()
Y = deque()
U = deque()


def rotate():
    degLis = ["U", "R", "D", "L", "L", "U", "R", "D"]
    for num in range(N):
        tmpX = sideLen - Y[num]
        Y[num] = X[num]
        X[num] = tmpX
        U[num] = degLis[degLis.index(U[num])+4]


for _ in range(N):
    x, y, u = map(str, input().split())
    X.append(int(x))
    Y.append(int(y))
    U.append(u)
ans = sideLen
for i in range(4):
    if i >= 2:
        revNum = solve_reverse()
        ans = min(revNum, ans)
    degNum = solve_90degree()
    ans = min(degNum, ans)
    rotate()
if ans == sideLen:
    print("SAFE")
else:
    print(ans)

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):
    ret = INF
    lower = -INF
    upper = INF
    for c in up.keys() & down.keys():
        for v in sorted(up[c] | down[c]):
            if v in up[c]:
                lower = v
            else:
                upper = v
            if upper > lower:
                ret = min(ret, upper - lower)
    return ret


#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):
    ret = INF
    lower = -INF
    upper = INF
    for c in up.keys() & down.keys():
        for v in sorted(up[c] | down[c]):
            if v in down[c]:
                upper = v
            else:
                lower = v
            if upper > lower:
                ret = min(ret, upper - lower)
    return ret


#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):
    ret = INF
    lower = -INF
    upper = INF
    for c in up.keys() & down.keys():
        for v, d in sorted({(v, 0)
                            for v in up[c]} | {(v, 1)
                                               for v in down[c]}):
            if d:
                upper = v
            else:
                lower = v
            if upper > lower:
                ret = min(ret, upper - lower)
    return ret


#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):
    ret = INF
    lower = -INF
    upper = INF
    for c in up.keys() & down.keys():
        for v, d in sorted({(v, 0) for v in up[c]} | {(v, 1) for v in down[c]}):
            if d:
                upper = v
            else:
                lower = v
            ret = min(ret, upper - lower)
    return ret


#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:
from collections import deque

sideLen = 200000


def solve_reverse():
    tmpAns = sideLen
    values = []
    for num in range(N):
        if not (U[num] == "R" or U[num] == "L"):
            continue
        else:
            values.append([Y[num], X[num], U[num]])
    values.sort()
    # print(values)
    for num in range(len(values)-1):
        if not (values[num][2] == "R" and values[num + 1][2] == "L") or values[num][0] != values[num+1][0]:
            continue
        else:
            tmpAns = min(tmpAns, values[num + 1][1] - values[num][1])
            #print(1, tmpAns, values[num+1], values[num])
    return tmpAns * 5


def solve_90degree():
    tmpAns = sideLen
    values = []
    for num in range(N):
        if not (U[num] == "R" or U[num] == "U"):
            continue
        else:
            values.append([X[num]+Y[num], X[num], U[num]])
    values.sort()
    for num in range(len(values)-1):
        if not (values[num][2] == "R" and values[num + 1][2] == "U") or values[num][0] != values[num+1][0]:
            continue
        else:
            tmpAns = min(tmpAns, values[num + 1][1] - values[num][1])
            #print(2, tmpAns, values[num+1], values[num])
    return tmpAns * 10


N = int(input())
X = deque()
Y = deque()
U = deque()


def rotate():
    degLis = ["U", "R", "D", "L", "L", "U", "R", "D"]
    for num in range(N):
        tmpX = sideLen - Y[num]
        Y[num] = X[num]
        X[num] = tmpX
        U[num] = degLis[degLis.index(U[num])+4]


for _ in range(N):
    x, y, u = map(str, input().split())
    X.append(int(x))
    Y.append(int(y))
    U.append(u)
ans = sideLen
for _ in range(4):
    revNum = solve_reverse()
    ans = min(revNum, ans)
    degNum = solve_90degree()
    ans = min(degNum, ans)
    rotate()
if ans == sideLen:
    print("SAFE")
else:
    print(ans)