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:
n=int(input())
u = []
d = []
r = []
l = []
for i in range(n):
    x=list(map(str,input().split()))
    if x[2]=="U":
        u.append(x)
    elif x[2]=="D":
        d.append(x)
    elif x[2]=="R":
        r.append(x)
    else:
        l.append(x)

t=[]

for i in r:
    for j in u:
        if (int(j[0])-int(i[0]))==(int(i[1])-int(j[1])) and (int(i[1])-int(j[1]))>0:
            t.append((int(i[1])-int(j[1]))*10)
    for j in d:
        if (int(i[0])-int(j[0]))==(int(j[1])-int(i[1])) and (int(j[1])-int(i[1]))>0:
            t.append((int(j[1])-int(i[1]))*10)
    for j in l:
        if i[1]==j[1] and (int(i[0])-int(j[0]))%2==0 and int(i[0])<int(j[0]):
            t.append((int(j[0])-int(i[0]))*5)
            
        
for i in l:
    for j in u:
        if (int(i[0])-int(j[0]))==(int(i[1])-int(j[1])) and (int(i[1])-int(j[1]))>0:
            t.append((int(i[1])-int(j[1]))*10)
    for j in d:
        if (int(i[0])-int(j[0]))==(int(j[1])-int(i[1])) and (int(j[1])-int(i[1]))>0:
            t.append((int(j[1])-int(i[1]))*10)

for i in u:
    for j in d:
        if i[0]==j[0] and (int(i[1])-int(j[1]))%2==0 and int(i[1])<int(j[1]):
            t.append((int(j[1])-int(i[1]))*5)
            
if len(t)==0:
    print("SAFE")
else:
    print(min(t))

Submission status: Wrong Answer

Solution code:
import numpy as np
ans = []
#
#f = open('./input_F.txt')
#
#N = int(f.readline())
#XY = []
#X = []
#Y = []
#U = []
#for i in range(N):
#    XYU = list(map(str, f.readline().split()))
#    XY.append([int(XYU[0]),int(XYU[1])])
#    X.append(int(XYU[0]))
#    Y.append(int(XYU[1]))
#    U.append(XYU[2])



N = int(input())
XY = []
U = []
X = []
Y=[]
for i in range(N):
    XYU = list(map(str, input().split()))
    XY.append([int(XYU[0]),int(XYU[1])])
    X.append(int(XYU[0]))
    Y.append(int(XYU[1]))
    U.append(XYU[2])
    
    
    
X_minus_Y = [X[i]-Y[i] for i in range(N)]
X_plus_Y = [X[i]+Y[i] for i in range(N)]

X_argsort = np.argsort(np.array(X))
Y_argsort = np.argsort(np.array(Y))
XmY_argsort = np.argsort(np.array(X_minus_Y))
XpY_argsort = np.argsort(np.array(X_plus_Y))

for i in range(N-1):
    Xi = X_argsort[i]
    Xii = X_argsort[i+1]
    Yi = Y_argsort[i]
    Yii = Y_argsort[i+1]
    XmYi = XmY_argsort[i]
    XmYii = XmY_argsort[i+1]
    XpYi = XpY_argsort[i]
    XpYii = XpY_argsort[i+1]
    
    if X[Xi]==X[Xii]:
        if Y[Xi]>Y[Xii] and U[Xi] == "D" and U[Xii] == "U":
            ans.append((Y[i]-Y[i+1])*10//2)
        elif Y[Xi]<Y[Xii] and U[Xi] == "U" and U[Xii] == "D":
            ans.append((Y[Xii]-Y[Xi])*10//2)
    
    if Y[Yi]==Y[Yii]:
        if X[Yi]>X[Yii] and U[Yi] == "L" and U[Yii] == "R":
            ans.append((X[Yi]-X[Yii])*10//2)
        elif X[Yi]<X[Yii] and U[Yi] == "R" and U[Yii] == "L":
            ans.append((X[Yii]-X[Yi])*10//2)

    if X_minus_Y[XmYi]==X_minus_Y[XmYii]:
        if X[XmYi]>X[XmYii]:
            if U[XmYi] == "D" and U[XmYii] == "R":
                ans.append((X[XmYi]-X[XmYii])*10)
            if U[XmYi] == "L" and U[XmYii] == "U":
                ans.append((X[XmYi]-X[XmYii])*10)                
        elif X[XmYi]<X[XmYii]:
            if U[XmYi] == "R" and U[XmYii] == "D":
                ans.append((X[XmYi]-X[XmYii])*10*(-1))
            if U[XmYi] == "U" and U[XmYii] == "L":
                ans.append((X[XmYi]-X[XmYii])*10*(-1))
                
    if X_plus_Y[XpYi]==X_plus_Y[XpYii]:
        if X[XpYi]>X[XpYii]:
            if U[XpYi] == "U" and U[XpYii] == "R":
                ans.append((X[XpYi]-X[XpYii])*10)
            if U[XpYi] == "L" and U[XpYii] == "D":
                ans.append((X[XpYi]-X[XpYii])*10)                
        elif X[XpYi]<X[XpYii]:
            if U[XpYi] == "D" and U[XpYii] == "L":
                ans.append((X[XpYi]-X[XpYii])*10*(-1))
            if U[XpYi] == "R" and U[XpYii] == "U":
                ans.append((X[XpYi]-X[XpYii])*10*(-1))
                
#XYarray = np.array([X,Y,X_minus_Y,X_plus_Y]).T
if len(ans)==0:
    final_ans = "SAFE"
else:
    final_ans = min(ans)


print(final_ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
X = []
Y = []
U = []
time = []
for i in range(N):
  xappend, yappend, uappend = input().split()
  xappend = int(xappend)
  yappend = int(yappend)
  X.append(xappend)
  Y.append(yappend)
  U.append(uappend)
  if i > 0:
    for j in range(i):
      if U[i] == "U" and U[j] == "D":
        if Y[i] < Y[j] and X[i] == X[j]:
          time.append((Y[j]-Y[i])*5)

      elif U[i] == "D" and U[j] == "U":
          if Y[i] > Y[j] and X[i] == X[j]:
            time.append((Y[i]-Y[j])*5)

      elif U[i] == "R" and U[j] == "L":
        if X[i] < X[j] and Y[i] == Y[j]:
          time.append((X[j]-X[i])*5)

      elif U[i] == "L" and U[j] == "R":
        if X[i] > X[j] and Y[i] == Y[j]:
          time.append((X[i]-X[j])*5)

      elif U[i] == "U" and U[j] == "R":
        if X[i] > X[j] and Y[i] < Y[j] and X[i]-X[j] == Y[j]-Y[i]:
          time.append((X[i]-X[j])*10)

      elif U[i] == "U" and U[j] == "L":
        if X[i] < X[j] and Y[i] < Y[j] and X[j]-X[i] == Y[j]-Y[i]:
          time.append((X[j]-X[i])*10)

      elif U[i] == "D" and U[j] == "R":
        if X[i] > X[j] and Y[i] > Y[j] and X[i]-X[j] == Y[i]-Y[j]:
          time.append((X[i]-X[j])*10)

      elif U[i] == "D" and U[j] == "L":
        if X[i] < X[j] and Y[i] > Y[j] and X[j]-X[i] == Y[j]-Y[i]:
          time.append((X[j]-X[i])*10)

      elif U[i] == "R" and U[j] == "U":
        if X[i] < X[j] and Y[i] > Y[j] and X[j]-X[i] == Y[i]-Y[j]:
          time.append((X[j]-X[i])*10)

      elif U[i] == "R" and U[j] == "D":
        if X[i] < X[j] and Y[i] < Y[j] and X[j]-X[i] == Y[j]-Y[i]:
          time.append((X[i]-X[j])*10)

      elif U[i] == "L" and U[j] == "U":
        if X[i] < X[j] and Y[i] < Y[j] and X[i]-X[j] == Y[i]-Y[j]:
          time.append((X[j]-X[i])*10)

      elif U[i] == "L" and U[j] == "D":
        if X[i] > X[j] and Y[i] < Y[j] and X[i]-X[j] == Y[j]-Y[i]:
          time.append((X[i]-X[j])*10)

if len(time) > 0:
  time.sort()
  print(time[0])
else:
  print("SAFE")

Submission status: Wrong Answer

Solution code:
import collections
import heapq
def main():

    N = int(input())

    RL = collections.defaultdict(list)
    UD = collections.defaultdict(list)
    UL = collections.defaultdict(list)
    UR = collections.defaultdict(list)
    DL = collections.defaultdict(list)
    DR = collections.defaultdict(list)
    for _ in range(N):
        X, Y, U = input().split()
        X = int(X)
        Y = int(Y)
        if U == 'R':
            heapq.heappush(RL[Y], [X, 0])
            heapq.heappush(UR[X+Y], [X, 0])
            heapq.heappush(DR[X-Y], [X, 0])
        elif U == 'L':
            heapq.heappush(RL[Y], [X, 1])
            heapq.heappush(UL[X-Y], [X, 1])
            heapq.heappush(DL[X+Y], [X, 1])
        elif U == 'U':
            heapq.heappush(UD[X], [Y, 0])
            heapq.heappush(UL[X-Y], [X, 0])
            heapq.heappush(UR[X+Y], [X, 1])
        else:
            heapq.heappush(UD[X], [Y, 1])
            heapq.heappush(DL[X+Y], [X, 0])
            heapq.heappush(DR[X-Y], [X, 1])
    # print(horizontal)
    # print(vertical)

    ans = float('inf')
    for V in [UL, UR, DL, DR]:
        for X in V:
            for j in range(len(V[X])-1):
                if V[X][j][1] == 0 and V[X][j+1][1] == 1:
                    ans = min(ans, 10 * (V[X][j+1][0]- V[X][j][0]))
    for V in [UD, RL]:
        for X in V:
            for j in range(len(V[X])-1):
                if V[X][j][1] == 0 and V[X][j+1][1] == 1:
                    ans = min(ans, 5 * (V[X][j+1][0]- V[X][j][0]))

    if ans == float('inf'): return "SAFE"
    return ans

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
N = int(input())
plane = [0] * N
houkou = ["U", "D", "R", "L"]
up = []
down = []
right = []
left =[]
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]
  
#x座標が同じでぶつかる
plane = sorted(plane, key=lambda x:x[0])
time = 10 ** 10
for k in range(1, N):
  one = plane[k]
  j = 1
  T = True
  while T:  
    two = plane[k - j]  
    if one[0] == two[0]:
      if one[1] > two[1]:
        if (one[4] == 1) and (two[4] == 0):
          time2 = (one[1] - two[1]) / 0.2
          time = min(time, time2)
      else:
        if (one[4] == 0) and (two[4] == 1):
          time2 = (two[1] - one[1]) / 0.2
          time = min(time, time2)
    else:
      T = False
    j += 1
    if k - j == -1:
      T = False
      
#y座標が同じでぶつかる
for k in range(1, N):
  one = plane[k]
  j = 1
  T = True
  while T:  
    two = plane[k - j]  
    if one[1] == two[1]:
      if one[0] > two[0]:
        if (one[4] == 3) and (two[4] == 2):
          time2 = (one[0] - two[0]) / 0.2
          time = min(time, time2)
      else:
        if (one[4] == 2) and (two[4] == 3):
          time2 = (two[0] - one[0]) / 0.2
          time = min(time, time2)
    else:
      T = False
    j += 1
    if k - j == -1:
      T = False
      

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:
import itertools

def main():
    n = int(input())
    ans = 10**9
    u = []
    r = []
    d = []
    l = []
    for _ in range(n):
        k = input().split()
        x,y = int(k[0]),int(k[1])
        if(k[2] == "U"):
            u.append((x,y))
        elif(k[2] == "R"):
            r.append((x,y))
        elif(k[2] == "D"):
            d.append((x,y))
        elif(k[2] == "L"):
            l.append((x,y))

    prd = list(itertools.product(l,r))
    for j in prd:
        one = j[0]
        two = j[1]
        if(one[1] == two[1] and one[0] > two[0]):
            dif = one[0] - two[0]
            ans = min(ans,dif*10 //2)
            
    prd = list(itertools.product(u,d))
    for j in prd:
        one = j[0]
        two = j[1]
        if(one[0] == two[0] and one[1] < two[1]):
            dif = - one[1] + two[1]
            ans = min(ans,dif* 10 //2)

    prd = list(itertools.product(u,r))
    for j in prd:
        one = j[0]
        two = j[1]
        if(one[0]-two[0] == two[1]-one[1] and one[0]-two[0] > 0 and one[1] - two[1] < 0):
            ans = min(ans,(one[0]-two[0])*10)

    prd = list(itertools.product(u,l))
    for j in prd:
        one = j[0]
        two = j[1]
        if(-one[0] + two[0] == one[1] - two[1] and one[0]-two[0] < 0 and one[1] - two[1] < 0):
            ans = min(ans,(two[0]-one[0])*10)
    
    prd = list(itertools.product(d,r))
    for j in prd:
        one = j[0]
        two = j[1]
        if(one[0]-two[0] == one[1]-two[1] and one[0]-two[0] > 0 and one[1] - two[1] < 0):
            ans = min(ans,(-two[0] + one[0])*10)

    prd = list(itertools.product(d,l))
    for j in prd:
        one = j[0]
        two = j[1]
        if(two[0]-one[0] == one[1] - two[1] and one[0]-two[0] < 0 and one[1] - two[1] > 0):
            ans = min(ans,(two[0] - one[0])*10)
    if(ans == 10**9):
        print("SAFE")
        return
    else:
        print(ans)
        return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
xyu=[list(input().split()) for i in range(n)]
for i in range(n):
    x,y,u=xyu[i]
    xyu[i]=[int(x),int(y),u]

xyu.sort(lambda x: (x[0],x[1]))
tate=[-1]*200001
yoko=[-1]*200001
ans=float('inf')
for i in range(n):
    x,y,u=xyu[i]
    if u=="D":
        if tate[x]==-1:
            continue
        else:
            ans=min(ans,(y-tate[x])*5)
    else:
        tate[x]=y
# print(ans)
xyu.sort(lambda x: (x[1],x[0]))
# print(xyu)
for i in range(n):
    x,y,u=xyu[i]
    if u=="L":
        if yoko[y]==-1:
            continue
        else:
            ans=min(ans,(x-yoko[y])*5)
    else:
        yoko[y]=x
# print(ans)

dic={"U":0,"R":1,"D":2,"L":3}
cnt=[[] for i in range(2*200001)]
for i in range(n):
    x,y,u=xyu[i]
    cnt[x+y].append(i)
# print(cnt)

for i in range(len(cnt)):
    ul=[-1]*2
    if len(cnt[i])==0:
        continue
    cnt[i].sort(lambda x: xyu[x][1])
    # print(cnt[i])
    for j in range(len(cnt[i])):
        x,y,u=xyu[cnt[i][j]]
        if u=="U":
            ul[0]=y
        elif u=="L":
            ul[1]=y
        elif u=="D":
            if ul[1]!=-1:
                ans=min(ans,(y-ul[1])*10)
        elif u=="R":
            if ul[0]!=-1:
                ans=min(ans,(y-ul[0])*10)
        # print(urdl)
    
cnt=[[] for i in range(2*200001)]
# cnt=[[] for i in range(60)]
for i in range(n):
    x,y,u=xyu[i]
    # print(200000-x+y,u)
    cnt[200000-x+y].append(i)
# print()
for i in range(len(cnt)):
    ul=[-1]*2
    if len(cnt[i])==0:
        continue
    cnt[i].sort(lambda x: xyu[x][1])
    # print(cnt[i])
    for j in range(len(cnt[i])):
        x,y,u=xyu[cnt[i][j]]
        if u=="U":
            ul[0]=y
        elif u=="R":
            ul[1]=y
        elif u=="D":
            if ul[1]!=-1:
                ans=min(ans,(y-ul[1])*10)
        elif u=="L":
            if ul[0]!=-1:
                ans=min(ans,(y-ul[0])*10)
        # print(urdl)
# print(ans)
if ans!=float('inf'):
    print(ans)
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

def dist1(C):
    # (x, y, u)    
    C.sort()
    #print(C)
    for (x0, y0, u0), (x1, y1, u1) in zip(C, C[1:]):
        if (u0, u1) in (("U", "R"), ("L", "D"), ("U", "L"), ("R", "D"), ("U", "D"), ("R", "L")):
            yield abs(x0 - x1) * 10
    yield float("inf")

def dist2(C):
    # (x, y, u)    
    C.sort()
    #print(C)
    for (x0, y0, u0), (x1, y1, u1) in zip(C, C[1:]):
        if (u0, u1) in (("U", "D"), ("R", "L")):
            yield max(abs(x0 - x1), abs(y0 - y1)) * 5
    yield float("inf")
    
def solve1(N: int, X: "List[int]", Y: "List[int]", U: "List[str]"):
    from collections import defaultdict
    UR = defaultdict(list)
    DL = defaultdict(list)
    UL = defaultdict(list)        
    DR = defaultdict(list)
    D = {"U": [UR, UL], "D": [DL, DR], "L": [DL, UL], "R": [UR, DR]}
    for x, y, u in zip(X, Y, U):
        k0 = y + x
        k1 = y - x
        D[u][0][k0].append((x, y, u))
        D[u][1][k1].append((x, y, u))
    for d in [UR, DL, UL, DR]:
        for v in d.values():
            if len(v) <= 1:
                continue
            yield min(dist1(v))
    yield(float("inf"))

def solve2(N: int, X: "List[int]", Y: "List[int]", U: "List[str]"):
    from collections import defaultdict
    XX = defaultdict(list)
    YY = defaultdict(list)
    for x, y, u in zip(X, Y, U):
        XX[x].append((x, y, u))
        YY[y].append((x, y, u))
    for ZZ in [XX, YY]:
        for Z in ZZ.values():
            yield min(dist2(Z))
    yield(float("inf"))
            
def solve(N: int, X: "List[int]", Y: "List[int]", U: "List[str]"):
    D = dict(zip("UDRL", "RLUD"))
    U = [D[u] for u in U]
    ans = min(min(solve1(N, X, Y, U)), min(solve2(N, X, Y, U)))
    return ans if ans != float("inf") else "SAFE"

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    X = [int()] * (N)  # type: "List[int]"
    Y = [int()] * (N)  # type: "List[int]"
    U = [str()] * (N)  # type: "List[str]"
    for i in range(N):
        X[i] = int(next(tokens))
        Y[i] = int(next(tokens))
        U[i] = next(tokens)
    print(solve(N, X, Y, U))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
plane = [0] * N
houkou = ["U", "D", "R", "L"]
up = []
down = []
right = []
left =[]
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]
  
#x座標が同じでぶつかる
plane = sorted(plane, key=lambda x:x[0])
time = 10 ** 10
for k in range(1, N):
  one = plane[k]
  j = 1
  T = True
  while T:  
    two = plane[k - j]  
    if one[0] == two[0]:
      if one[1] > two[1]:
        if (one[4] == 1) and (two[4] == 0):
          time2 = (one[1] - two[1]) / 0.2
          time = min(time, time2)
          T = False
      else:
        if (one[4] == 0) and (two[4] == 1):
          time2 = (two[1] - one[1]) / 0.2
          time = min(time, time2)
          T = False
    else:
      T = False
    j += 1
    if k - j == -1:
      T = False
      
#y座標が同じでぶつかる
for k in range(1, N):
  one = plane[k]
  j = 1
  T = True
  while T:  
    two = plane[k - j]  
    if one[1] == two[1]:
      if one[0] > two[0]:
        if (one[4] == 3) and (two[4] == 2):
          time2 = (one[0] - two[0]) / 0.2
          time = min(time, time2)
          T = False
      else:
        if (one[4] == 2) and (two[4] == 3):
          time2 = (two[0] - one[0]) / 0.2
          time = min(time, time2)
          T = False
    else:
      T = False
    j += 1
    if k - j == -1:
      T = False
      

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:
ans = "SAFE"

import numpy as np
#import collections
#import math
#
#f = open('./input_F.txt')
#
#N = int(f.readline())
#XY = []
#X = []
#Y = []
#U = []
#for i in range(N):
#    XYU = list(map(str, f.readline().split()))
#    XY.append([int(XYU[0]),int(XYU[1])])
#    X.append(int(XYU[0]))
#    Y.append(int(XYU[1]))
#    U.append(XYU[2])



N = int(input())
XY = []
U = []
X = []
Y=[]
for i in range(N):
    XYU = list(map(str, input().split()))
    XY.append([int(XYU[0]),int(XYU[1])])
    X.append(int(XYU[0]))
    Y.append(int(XYU[1]))
    U.append(XYU[2])

X_minus_Y = [X[i]-Y[i] for i in range(N)]
X_plus_Y = [X[i]+Y[i] for i in range(N)]

X_argsort = np.argsort(np.array(X))
Y_argsort = np.argsort(np.array(Y))
XmY_argsort = np.argsort(np.array(X_minus_Y))
XpY_argsort = np.argsort(np.array(X_plus_Y))

for i in range(N-1):
    Xi = X_argsort[i]
    Xii = X_argsort[i+1]
    Yi = Y_argsort[i]
    Yii = Y_argsort[i+1]
    XmYi = XmY_argsort[i]
    XmYii = XmY_argsort[i+1]
    XpYi = XpY_argsort[i]
    XpYii = XpY_argsort[i+1]
    
    if X[Xi]==X[Xii]:
        if Y[Xi]>Y[Xii] and U[Xi] == "D" and U[Xii] == "U":
            ans = (Y[i]-Y[i+1])*10//2
        elif Y[Xi]<Y[Xii] and U[Xi] == "U" and U[Xii] == "D":
            ans = (Y[Xii]-Y[Xi])*10//2
    
    if Y[Yi]==Y[Yii]:
        if X[Yi]>X[Yii] and U[Yi] == "L" and U[Yii] == "R":
            ans = (X[Yi]-X[Yii])*10//2
        elif X[Yi]<X[Yii] and U[Yi] == "R" and U[Yii] == "L":
            ans = (X[Yii]-X[Yi])*10//2    

    if X_minus_Y[XmYi]==X_minus_Y[XmYii]:
        if X[XmYi]>X[XmYii]:
            if U[XmYi] == "D" and U[XmYii] == "R":
                ans = (X[XmYi]-X[XmYii])*10
            if U[XmYi] == "L" and U[XmYii] == "U":
                ans = (X[XmYi]-X[XmYii])*10                
        elif X[XmYi]<X[XmYii]:
            if U[XmYi] == "R" and U[XmYii] == "D":
                ans = (X[XmYi]-X[XmYii])*10*(-1)
            if U[XmYi] == "U" and U[XmYii] == "L":
                ans = (X[XmYi]-X[XmYii])*10*(-1)
                
    if X_plus_Y[XpYi]==X_plus_Y[XpYii]:
        if X[XpYi]>X[XpYii]:
            if U[XpYi] == "U" and U[XpYii] == "R":
                ans = (X[XpYi]-X[XpYii])*10
            if U[XpYi] == "L" and U[XpYii] == "D":
                ans = (X[XpYi]-X[XpYii])*10                
        elif X[XpYi]<X[XpYii]:
            if U[XpYi] == "D" and U[XpYii] == "L":
                ans = (X[XpYi]-X[XpYii])*10*(-1)
            if U[XpYi] == "R" and U[XpYii] == "U":
                ans = (X[XpYi]-X[XpYii])*10*(-1)
                
#XYarray = np.array([X,Y,X_minus_Y,X_plus_Y]).T

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
u_plane = []
d_plane = []
l_plane = []
r_plane = []
for i in range(n):
  x,y,u = input().split()
  x, y = int(x), int(y)
  if u == "U":
    u_plane.append([x,y])
  elif u == "D":
    d_plane.append([x,y])
  elif u == "L":
    l_plane.append([x,y])
  else:
    r_plane.append([x,y])

time = pow(10,15)
for i in u_plane:
  for j in d_plane:
    if i[0] == j[0] and i[1] < j[1]:
      time = min(time, abs(i[1] - j[1]) / 2)
  for j in l_plane:
    if i[0] < j[0] and i[1] < j[1]:
      crosspoint_x = i[0]
      crosspoint_y = j[1]
      time_1 = abs(crosspoint_x - j[0])
      time_2 = abs(crosspoint_y - i[1])
      if time_1 == time_2:
        time = min(time,time_1)
  for j in r_plane:
    if i[0] > j[0] and i[1] < j[1]:
      crosspoint_x = i[0]
      crosspoint_y = j[1]
      time_1 = abs(crosspoint_x - j[0])
      time_2 = abs(crosspoint_y - i[1])
      if time_1 == time_2:
        time = min(time,time_1)
        
for i in d_plane:
  for j in l_plane:
    if i[0] < j[0] and i[1] > j[1]:
      crosspoint_x = i[0]
      crosspoint_y = j[1]
      time_1 = abs(crosspoint_x - j[0])
      time_2 = abs(crosspoint_y - i[1])
      if time_1 == time_2:
        time = min(time,time_1)
  for j in r_plane:
    if i[0] > j[0] and i[1] > j[1]:
      crosspoint_x = i[0]
      crosspoint_y = j[1]
      time_1 = abs(crosspoint_x - j[0])
      time_2 = abs(crosspoint_y - i[1])
      if time_1 == time_2:
        time = min(time,time_1)
        
# lとrの追突
for i in r_plane:
  for j in l_plane:
    if i[1] == j[1] and i[0] < j[1]:
      time = min(time, abs(i[0] - j[0])/2)
      
if time == pow(10,15):
  print("SAFE")
else:
  print(int(time*10))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N=int(input())

udic_x=defaultdict(set)
udic_xpy=defaultdict(set)
udic_xmy=defaultdict(set)
ddic_x=defaultdict(set)
ddic_xpy=defaultdict(set)
ddic_xmy=defaultdict(set)
ldic_y=defaultdict(set)
ldic_xpy=defaultdict(set)
ldic_xmy=defaultdict(set)
rdic_y=defaultdict(set)
rdic_xpy=defaultdict(set)
rdic_xmy=defaultdict(set)

for _ in range(N):
  XS,YS,U=input().split()
  x=int(XS)
  y=int(YS)
  if U=="U":
    udic_x[x].add(y)
    udic_xpy[x+y].add(x)
    udic_xmy[x-y].add(x)
  elif U=="D":
    ddic_x[x].add(y)
    ddic_xpy[x+y].add(x)
    ddic_xmy[x-y].add(x)
  elif U=="L":
    ldic_y[y].add(x)
    ldic_xpy[x+y].add(x)
    ldic_xmy[x-y].add(x)
  else:
    rdic_y[y].add(x)
    rdic_xpy[x+y].add(x)
    rdic_xmy[x-y].add(x)
    
answer=float("inf")
#UD
for x in udic_x:
  yset_u=udic_x[x]
  yset_d=ddic_x[x]  
  #print(yset_u,yset_d)
  for yu in yset_u:
    for yd in yset_d:
      #print(yu,yd)
      if yu<yd:
        answer=min(answer,(yd-yu)*5)
#LR
for y in ldic_y:
  xset_l=ldic_y[y]
  xset_r=rdic_y[y]  
  #print(xset_l,xset_r)
  for xl in xset_l:
    for xr in xset_r:
      #print(xl,xr)
      if xr<xl:
        answer=min(answer,(xl-xr)*5)
        
#UL
for xy in udic_xmy:
  xyset_u=udic_xmy[xy]
  xyset_l=ldic_xmy[xy]
  #print(xy,xyset_u,xyset_l)
  for xu in xyset_u:
    for xl in xyset_l:
      if xl-xu>0:
        answer=min(answer,(xl-xu)*10)
#UR
for xy in udic_xpy:
  xyset_u=udic_xpy[xy]
  xyset_r=rdic_xpy[xy]
  #print(xy,xyset_u,xyset_r)
  for xu in xyset_u:
    for xr in xyset_r:
      if xu-xr>0:
        answer=min(answer,(xu-xr)*10)
#DL
for xy in ddic_xpy:
  xyset_d=ddic_xpy[xy]
  xyset_l=ldic_xpy[xy]
  #print(xy,xyset_d,xyset_l)
  for xd in xyset_d:
    for xl in xyset_l:
      if xl-xd>0:
        answer=min(answer,(xl-xd)*10)
#DR
for xy in ddic_xpy:
  xyset_d=ddic_xmy[xy]
  xyset_r=rdic_xmy[xy]
  #print(xy,xyset_d,xyset_l)
  for xd in xyset_d:
    for xr in xyset_l:
      if xd-xr>0:
        answer=min(answer,(xd-xr)*10)
  
if answer==float("inf"):
  print("SAFE")
else:
  print(answer)