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())
X = [0]*N
Y = [0]*N
U = [0]*N
const = 400010
const2 = 200010
S_U = [[] for i in range(const)]
S_R = [[] for i in range(const)]
S_D = [[] for i in range(const)]
S_L = [[] for i in range(const)]

D_U = [[] for i in range(const)]
D_R = [[] for i in range(const)]
D_D = [[] for i in range(const)]
D_L = [[] for i in range(const)]

UD_U = [[] for i in range(const2)]
UD_D = [[] for i in range(const2)]
LR_L = [[] for i in range(const2)]
LR_R = [[] for i in range(const2)]

for i in range(N):
    X[i],Y[i],U[i] = input().split()
    X[i] = int(X[i])
    Y[i] = int(Y[i])
    if U[i]=="U":
        S_U[X[i]+Y[i]].append(X[i])
        D_U[X[i]-Y[i]+200000].append(X[i])
        UD_U[X[i]].append(Y[i])
    elif U[i]=="R":
        S_R[X[i]+Y[i]].append(X[i])
        D_R[X[i]-Y[i]+200000].append(X[i])
        LR_R[Y[i]].append(X[i])
    elif U[i]=="D":
        S_D[X[i]+Y[i]].append(X[i])
        D_D[X[i]-Y[i]+200000].append(X[i])
        UD_D[X[i]].append(Y[i])
    else:
        S_L[X[i]+Y[i]].append(X[i])
        D_L[X[i]-Y[i]+200000].append(X[i])
        LR_L[Y[i]].append(X[i])
        
        
import itertools

safe = 0
time = 1000000
for i in range(const2):
    if UD_U[i] != [] and UD_D[i] != []:

        for temp in list(itertools.product(UD_U[i], UD_D[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1])//2,time)

    if LR_L[i] != [] and LR_R[i] != []:

        for temp in list(itertools.product(LR_L[i], LR_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1])//2,time)

for i in range(const):
    if S_U[i] != [] and S_R[i] != []:

        for temp in list(itertools.product(S_U[i], S_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if S_D[i] != [] and S_L[i] != []:

        for temp in list(itertools.product(S_D[i], S_L[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if D_U[i] != [] and D_L[i] != []:

        for temp in list(itertools.product(D_U[i], D_L[i])):
            if temp[1]-temp[0]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)
            
    if D_D[i] != [] and S_R[i] != []:

        for temp in list(itertools.product(D_D[i], S_R[i])):
            if temp[0]-temp[1]>0:
                safe += 1
                time = min(abs(temp[0]-temp[1]),time)

if safe == 0:
    print("SAFE")
else:
    print(time*10)

Submission status: Wrong Answer

Solution code:
n = int(input())
x = [0]*n
y = [0]*n

ux = [0]*n
uy = [0]*n


for i in range(n):
    px,py,u =  input().split()
    if u=="U":
        uy[i] =1
    elif u=="R":
        ux[i] =1
    elif u=="D":
        uy[i] =-1
    elif u=="L":
        ux[i] =-1
    x[i]= int(px)
    y[i]= int(py)
time = 2*10**7
for i in range(n):
    for j in range(n-1-i):

            if x[i]-x[i+1+j] == 0 and  y[i]-y[i+1+j] == 0:
                time = min(time,0)
            elif x[i]-x[i+1+j] == 0 and uy[i]*uy[i+1+j]==-1 and (y[i]-y[i+1+j])*uy[i]<0:
                time = min(time,abs((y[i]-y[i+1+j])/0.2))
            elif y[i]-y[i+1+j] == 0 and ux[i]*ux[i+1+j]==-1 and (x[i]-x[i+1+j])*ux[i]<0:
                time = min(time,abs((x[i]-x[i+1+j])/0.2))
            elif (ux[i]-ux[i+1+j])==0 or (uy[i]-uy[i+1+j])==0:
                time =time
            elif (x[i]-x[i+1+j]) /(ux[i]-ux[i+1+j]) ==(y[i]-y[i+1+j]) /(uy[i]-uy[i+1+j]):
                time = min(time,abs((x[i]-x[i+1+j])/0.1))

if time == 2*10**7:
    print("SAFE")
else:
    print(int(time))

Submission status: Wrong Answer

Solution code:
import math
N = int(input())   #入力
X = [0] * N
Y = [0] * N
U = ['0'] * N
for i in range(N): 
    a = list(input().split())
    
    X[i] = (int(a[0]))
    Y[i] = (int(a[1]))
    U[i] = (str(a[2]))

if max(X) > max(Y):
    max =  max(X)
else:
    max = max(Y)
    
message = "SAFE"
flg = False
for i in range(max):
    for index in range(N): #移動
        if U[index] == 'U':
            Y[index] += 1
        if U[index] == 'R':
            X[index] += 1
        if U[index] == 'D':
            Y[index] -= 1
        if U[index] == 'L':
            X[index] -= 1
        
    for j in range(N):  #判定
        for k in range(N):
            if (X[j]==X[k] and Y[j]==Y[k] and j!=k):
                message = str((i+1)*10)
                flg = True
                break
            
    if flg==True:
        break

print(message)

Submission status: Wrong Answer

Solution code:
import math
N = int(input())   #入力
X = [0] * N
Y = [0] * N
U = ['0'] * N
for i in range(N): 
    a = list(input().split())
    
    X[i] = (int(a[0]))
    Y[i] = (int(a[1]))
    U[i] = (str(a[2]))


message = "SAFE"
flg = False
for i in range(20001):
    for index in range(N): #移動
        if U[index] == 'U':
            Y[index] += 1
        if U[index] == 'R':
            X[index] += 1
        if U[index] == 'D':
            Y[index] -= 1
        if U[index] == 'L':
            X[index] -= 1
        
    for j in range(N):  #判定
        for k in range(N):
            if (X[j]==X[k] and Y[j]==Y[k] and j!=k):
                message = str((i+1)*10)
                flg = True
                break
            
    if flg==True:
        break

print(message)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    inf = 10**10
    ans = inf
    u_list, d_list, l_list, r_list = [], [], [], []
    for _ in range(n):
        x, y, d = input().split()
        x, y = int(x), int(y)
        if d == "D":
            d_list.append((x, y))
        elif d == "R":
            r_list.append((x, y))
        elif d == "L":
            l_list.append((x, y))
        else:
            u_list.append((x, y))
    ld, lr, ll, lu = len(d_list), len(r_list), len(l_list), len(u_list)
    # 1→u,d
    u_list.sort()
    d_list.sort()
    i, j = 0, 0
    while(i < lu and j < ld):
        x, y = u_list[i]
        p, q = d_list[j]
        v = p-x
        w = (q-y)*5
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    # 2→r,l
    r_list.sort(key=lambda x: x[1])
    l_list.sort(key=lambda x: x[1])
    i, j = 0, 0
    while(i < lr and j < ll):
        x, y = r_list[i]
        p, q = l_list[j]
        v = q-y
        w = (p-x)*5
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    # 3→u,l
    u_list.sort(key=lambda x: x[0]-x[1])
    l_list.sort(key=lambda x: x[0]-x[1])
    i, j = 0, 0
    while(i < lu and j < ll):
        x, y = u_list[i]
        p, q = l_list[j]
        v = (p-q)-(x-y)
        w = (p-x)*10
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    # 4→d,l
    d_list.sort(key=lambda x: x[0]+x[1])
    l_list.sort(key=lambda x: x[0]+x[1])
    i, j = 0, 0
    while(i < ld and j < ll):
        x, y = d_list[i]
        p, q = l_list[j]
        v = (p+q)-(x+y)
        w = (p-x)*10
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    # 5→u,r
    u_list.sort(key=lambda x: (x[0]+x[1], -x[0]))
    r_list.sort(key=lambda x: (x[0]+x[1], -x[0]))
    i, j = 0, 0
    while(i < lu and j < lr):
        x, y = u_list[i]
        p, q = r_list[j]
        v = (p+q)-(x+y)
        w = (x-p)*10
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                j += 1
            else:
                i += 1
    # 6→d,r
    d_list.sort(key=lambda x: (x[0]-x[1], -x[0]))
    r_list.sort(key=lambda x: (x[0]-x[1], -x[0]))
    i, j = 0, 0
    while(i < ld and j < lr):
        x, y = d_list[i]
        p, q = r_list[j]
        v = (p-q)-(x-y)
        w = (x-p)*10
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                j += 1
            else:
                i += 1
    if ans == inf:
        print("SAFE")
    else:
        print(ans)


main()

Submission status: Wrong Answer

Solution code:
n=int(input())
x,y,u,X,Y=[],[],[],[],[]
p,q=[],[]
ans=100000000
s=1000000001
for i in range(n):
    x1,y1,u1=map(str,input().split())
    x1=int(x1)
    y1=int(y1)
    x.append([x1,y1])
    y.append([y1,x1])
    X.append([x1,y1,u1])
    Y.append([y1,x1,u1])
    u.append(u1)
X.sort()
Y.sort()
def has_duplicates2(seq):
    seen = []
    unique_list = [x for x in seq if x not in seen and not seen.append(x)]
    return len(seq) != len(unique_list)
for i in range(n):
    a=x[i][0]+x[i][1]
    b=max(x[i][0],x[i][1])-min(x[i][0],x[i][1])
    p.append([a,x[i][0],i])
    q.append([b,x[i][0],i])
p.sort()
q.sort()
if n==1:
    print("SAFE")
else:
    for i in range(n-1):
        if p[i][0]==p[i+1][0]:
            if (p[i][1]>p[i+1][1] and ((u[p[i][2]]=="U" and u[p[i+1][2]]=="R") or (u[p[i][2]]=="L" and u[p[i+1][2]]=="D"))) or  (p[i][1]<p[i+1][1] and ((u[p[i][2]]=="R" and u[p[i+1][2]]=="U") or (u[p[i][2]]=="D" and u[p[i+1][2]]=="L"))):
                s=abs(p[i][1]-p[i+1][1])
                if ans>s:
                    ans=s
        if q[i][0]==q[i+1][0]:
            if (q[i][1]>q[i+1][1] and ((u[q[i][2]]=="D" and u[q[i+1][2]]=="R") or (u[p[i][2]]=="L" and u[p[i+1][2]]=="U"))) or  (p[i][1]<p[i+1][1] and ((u[p[i][2]]=="R" and u[p[i+1][2]]=="D") or (u[p[i][2]]=="U" and u[p[i+1][2]]=="L"))):
                s=abs(q[i][1]-q[i+1][1])
                if ans>s:
                    ans=s
        if X[i][0]==X[i+1][0]:
            if (X[i][1]>X[i+1][1] and (X[i][2]=="D" and X[i+1][2]=="U")) or (X[i][1]<X[i+1][1] and (X[i][2]=="U" and X[i+1][2]=="D")):
                s=abs(X[i][1]-X[i+1][1])/2
                if ans>s:
                    ans=s
        if Y[i][0]==Y[i+1][0]:
            if (Y[i][1]>Y[i+1][1] and (Y[i][2]=="L" and Y[i+1][2]=="R")) or (Y[i][1]<Y[i+1][1] and (Y[i][2]=="R" and Y[i+1][2]=="L")):
                s=abs(Y[i][1]-Y[i+1][1])/2
                if ans>s:
                    ans=s
    if ans==100000000:
        print("SAFE")
    else:
        print(int(ans*10))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    inf = 10**10
    ans = inf
    u_list, d_list, l_list, r_list = [], [], [], []
    for _ in range(n):
        x, y, d = input().split()
        x, y = int(x), int(y)
        if d == "D":
            d_list.append((x, y))
        elif d == "R":
            r_list.append((x, y))
        elif d == "L":
            l_list.append((x, y))
        else:
            u_list.append((x, y))
    ld, lr, ll, lu = len(d_list), len(r_list), len(l_list), len(u_list)
    # 1→u,d
    u_list.sort()
    d_list.sort()
    i, j = 0, 0
    while(i < lu and j < ld):
        x, y = u_list[i]
        p, q = d_list[j]
        v = p-x
        w = (q-y)*5
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    # 2→r,l
    r_list.sort(key=lambda x: x[1])
    l_list.sort(key=lambda x: x[1])
    i, j = 0, 0
    while(i < lr and j < ll):
        x, y = r_list[i]
        p, q = l_list[j]
        v = q-y
        w = (p-x)*5
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    # 3→u,l
    u_list.sort(key=lambda x: x[0]-x[1])
    l_list.sort(key=lambda x: x[0]-x[1])
    i, j = 0, 0
    while(i < lu and j < ll):
        x, y = u_list[i]
        p, q = l_list[j]
        v = (p-q)-(x-y)
        w = (p-x)*10
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    # 4→d,l
    d_list.sort(key=lambda x: x[0]+x[1])
    l_list.sort(key=lambda x: x[0]+x[1])
    i, j = 0, 0
    while(i < ld and j < ll):
        x, y = d_list[i]
        p, q = l_list[j]
        v = (p+q)-(x+y)
        w = (p-x)*10
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    # 5→u,r
    u_list.sort(key=lambda x: (x[0]+x[1], -x[0]))
    r_list.sort(key=lambda x: (x[0]+x[1], -x[0]))
    i, j = 0, 0
    while(i < lu and j < lr):
        x, y = u_list[i]
        p, q = r_list[j]
        v = (p+q)-(x+y)
        w = (x-p)*10
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    # 6→d,r
    d_list.sort(key=lambda x: (x[0]-x[1], -x[0]))
    r_list.sort(key=lambda x: (x[0]-x[1], -x[0]))
    i, j = 0, 0
    while(i < ld and j < lr):
        x, y = d_list[i]
        p, q = r_list[j]
        v = (p-q)-(x-y)
        w = (x-p)*10
        if v > 0:
            i += 1
        elif v < 0:
            j += 1
        else:
            if w > 0:
                ans = min(ans, w)
                i += 1
            else:
                j += 1
    if ans == inf:
        print("SAFE")
    else:
        print(ans)


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[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[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:
n=int(input())
x,y,u,X,Y=[],[],[],[],[]
p,q=[],[]
ans=100000000
s=1000000001
for i in range(n):
    x1,y1,u1=map(str,input().split())
    x1=int(x1)
    y1=int(y1)
    x.append([x1,y1])
    y.append([y1,x1])
    X.append([x1,y1,u1])
    Y.append([y1,x1,u1])
    u.append(u1)
X.sort()
Y.sort()
def has_duplicates2(seq):
    seen = []
    unique_list = [x for x in seq if x not in seen and not seen.append(x)]
    return len(seq) != len(unique_list)
for i in range(n):
    a=x[i][0]+x[i][1]
    b=max(x[i][0],x[i][1])-min(x[i][0],x[i][1])
    p.append([a,x[i][0],i])
    q.append([b,x[i][0],i])
p.sort()
q.sort()
if n==1:
    print("SAFE")
else:
    for i in range(n-1):
        if p[i][0]==p[i+1][0]:
            if (p[i][1]>p[i+1][1] and ((u[p[i][2]]=="U" and u[p[i+1][2]]=="R") or (u[p[i][2]]=="L" and u[p[i+1][2]]=="D"))) or  (p[i][1]<p[i+1][1] and ((u[p[i][2]]=="R" and u[p[i+1][2]]=="U") or (u[p[i][2]]=="D" and u[p[i+1][2]]=="L"))):
                s=abs(p[i][1]-p[i+1][1])
                if ans>s:
                    ans=s
        if q[i][0]==q[i+1][0]:
            if (q[i][1]>q[i+1][1] and ((u[q[i][2]]=="D" and u[q[i+1][2]]=="R") or (u[p[i][2]]=="L" and u[p[i+1][2]]=="U"))) or  (p[i][1]<p[i+1][1] and ((u[p[i][2]]=="R" and u[p[i+1][2]]=="D") or (u[p[i][2]]=="U" and u[p[i+1][2]]=="L"))):
                s=abs(q[i][1]-q[i+1][1])
                if ans>s:
                    ans=s
        if X[i][0]==X[i+1][0]:
            if (X[i][1]>X[i+1][1] and (X[i][2]=="D" and X[i+1][2]=="U")) or (X[i][1]<X[i+1][1] and (X[i][2]=="U" and X[i+1][2]=="D")):
                s=abs(X[i][1]-X[i+1][1])/2
                if ans>s:
                    ans=s
        if Y[i][0]==Y[i+1][0]:
            if (Y[i][1]>Y[i+1][1] and (Y[i][2]=="L" and Y[i+1][2]=="R")) or (Y[i][1]<Y[i+1][1] and (Y[i][2]=="R" and Y[i+1][2]=="L")):
                s=abs(Y[i][1]-Y[i+1][1])/2
                if ans>s:
                    ans=s
    if ans==100000000:
        print("SAFE")
    else:
        print(ans*10)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [list(input().split()) for i in range(n)]
l = 2*10**5 + 5
# l = 5
# B = [[int(A[i][0]), int(A[i][1]), int(A[i][0])+int(A[i][1]), int(A[i][0])-int(A[i][1])+l, A[i][2]] for i in range(n)]

D = [[[] for j in range(2*l)] for i in range(4)]

# for i in range(4):
#     for j in range(n):
#         D[i][B[j][i]].append(B[j])

for i in range(n):
    x = int(A[i][0])
    y = int(A[i][1])
    nn = [x,y,A[i][2]]
    D[0][x].append(nn)
    D[1][y].append(nn)
    D[2][x+y].append(nn)
    D[3][x-y+l].append(nn)

# print(D)
import bisect
ans = float("inf")
for i in range(l):
    # print(D[1][i])
    LR = [[], []]
    for j in range(len(D[1][i])):
        
        if D[1][i][j][2] == "L":
            LR[0].append(D[1][i][j][0])
        if D[1][i][j][2] == "R":
            LR[1].append(D[1][i][j][0])
    # LR[0].sort()
    # LR[1].sort()
        # print(j,LR)
    for k in range(len(LR[1])):
        ind = bisect.bisect(LR[0], LR[1][k])
        if ind != len(LR[0]):
            ans = min(ans, (LR[0][ind]-LR[1][k]) * 5)

for i in range(l):
    # print(D[0][i])
    LR = [[], []]
    for j in range(len(D[0][i])):
        
        if D[0][i][j][2] == "U":
            LR[0].append(D[0][i][j][1])
        if D[0][i][j][2] == "D":
            LR[1].append(D[0][i][j][1])
    # LR[0].sort()
    # LR[1].sort()
        # print(j,LR)
    for k in range(len(LR[0])):
        ind = bisect.bisect(LR[1], LR[0][k])
        # print("ind",ind)
        if ind != len(LR[1]):
            ans = min(ans, (LR[1][ind]-LR[0][k]) * 5)

for i in range(2*l):
    # print(D[1][i])
    LR = [[], []]
    LR2 = [[], []]
    for j in range(len(D[2][i])):
        
        if D[2][i][j][2] == "L":
            LR[0].append(D[2][i][j][0])
        if D[2][i][j][2] == "D":
            LR[1].append(D[2][i][j][0])

        if D[2][i][j][2] == "U":
            LR2[0].append(D[2][i][j][0])
        if D[2][i][j][2] == "R":
            LR2[1].append(D[2][i][j][0])
        

    # print(j,LR2)
    # LR[0].sort()
    # LR2[0].sort()
    # LR[1].sort()
    # LR2[1].sort()

    for k in range(len(LR[1])):
        ind = bisect.bisect(LR[0], LR[1][k])
        if ind != len(LR[0]):
            ans = min(ans, (LR[0][ind]-LR[1][k]) * 10)
    
    for k in range(len(LR2[1])):
        ind = bisect.bisect(LR2[0], LR2[1][k])
        if ind != len(LR2[0]):
            ans = min(ans, (LR2[0][ind]-LR2[1][k]) * 10)

for i in range(2*l):
    # print(D[1][i])
    LR = [[], []]
    LR2 = [[], []]
    for j in range(len(D[3][i])):
        
        if D[3][i][j][2] == "D":
            LR[0].append(D[3][i][j][0])
        if D[3][i][j][2] == "R":
            LR[1].append(D[3][i][j][0])

        if D[3][i][j][2] == "L":
            LR2[0].append(D[3][i][j][0])
        if D[3][i][j][2] == "U":
            LR2[1].append(D[3][i][j][0])
    
    # LR[0].sort()
    # LR2[0].sort()
    # LR[1].sort()
    # LR2[1].sort()

        # print(j,LR)
    for k in range(len(LR[1])):
        ind = bisect.bisect(LR[0], LR[1][k])
        if ind != len(LR[0]):
            ans = min(ans, (LR[0][ind]-LR[1][k]) * 10)
    
    for k in range(len(LR2[1])):
        ind = bisect.bisect(LR2[0], LR2[1][k])
        if ind != len(LR2[0]):
            ans = min(ans, (LR2[0][ind]-LR2[1][k]) * 10)


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

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[Xi]-Y[Xii])*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 = int(min(ans))


print(final_ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [list(input().split()) for i in range(n)]
l = 2*10**5 + 5
# l = 500
# B = [[int(A[i][0]), int(A[i][1]), int(A[i][0])+int(A[i][1]), int(A[i][0])-int(A[i][1])+l, A[i][2]] for i in range(n)]

D = [[[] for j in range(2*l)] for i in range(4)]

# for i in range(4):
#     for j in range(n):
#         D[i][B[j][i]].append(B[j])

for i in range(n):
    x = int(A[i][0])
    y = int(A[i][1])
    nn = [x,y,A[i][2]]
    D[0][x].append(nn)
    D[1][y].append(nn)
    D[2][x+y].append(nn)
    D[3][x-y+l].append(nn)

# print(D)
import bisect
ans = float("inf")
for i in range(l):
    # print(D[1][i])
    LR = [[], []]
    for j in range(len(D[1][i])):
        
        if D[1][i][j][2] == "L":
            LR[0].append(D[1][i][j][0])
        if D[1][i][j][2] == "R":
            LR[1].append(D[1][i][j][0])

        # print(j,LR)
        for k in range(len(LR[1])):
            ind = bisect.bisect(LR[0], LR[1][k])
            if ind != len(LR[0]):
                ans = min(ans, (LR[0][ind]-LR[1][k]) * 5)

for i in range(l):
    # print(D[0][i])
    LR = [[], []]
    for j in range(len(D[0][i])):
        
        if D[0][i][j][2] == "U":
            LR[0].append(D[0][i][j][1])
        if D[0][i][j][2] == "D":
            LR[1].append(D[0][i][j][1])

        # print(j,LR)
        for k in range(len(LR[0])):
            ind = bisect.bisect(LR[1], LR[0][k])
            # print("ind",ind)
            if ind != len(LR[1]):
                ans = min(ans, (LR[1][ind]-LR[0][k]) * 5)

for i in range(2*l):
    # print(D[1][i])
    LR = [[], []]
    LR2 = [[], []]
    for j in range(len(D[2][i])):
        
        if D[2][i][j][2] == "L":
            LR[0].append(D[2][i][j][0])
        if D[2][i][j][2] == "D":
            LR[1].append(D[2][i][j][0])

        if D[2][i][j][2] == "U":
            LR2[0].append(D[2][i][j][0])
        if D[2][i][j][2] == "R":
            LR2[1].append(D[2][i][j][0])

        # print(j,LR)
        for k in range(len(LR[1])):
            ind = bisect.bisect(LR[0], LR[1][k])
            if ind != len(LR[0]):
                ans = min(ans, (LR[0][ind]-LR[1][k]) * 10)
        
        for k in range(len(LR2[1])):
            ind = bisect.bisect(LR2[0], LR2[1][k])
            if ind != len(LR2[0]):
                ans = min(ans, (LR2[0][ind]-LR2[1][k]) * 10)

for i in range(2*l):
    # print(D[1][i])
    LR = [[], []]
    LR2 = [[], []]
    for j in range(len(D[3][i])):
        
        if D[3][i][j][2] == "D":
            LR[0].append(D[3][i][j][1])
        if D[3][i][j][2] == "R":
            LR[1].append(D[3][i][j][1])

        if D[3][i][j][2] == "L":
            LR2[0].append(D[3][i][j][1])
        if D[3][i][j][2] == "U":
            LR2[1].append(D[3][i][j][1])

        # print(j,LR)
        for k in range(len(LR[1])):
            ind = bisect.bisect(LR[0], LR[1][k])
            if ind != len(LR[0]):
                ans = min(ans, (LR[0][ind]-LR[1][k]) * 10)
        
        for k in range(len(LR2[1])):
            ind = bisect.bisect(LR2[0], LR2[1][k])
            if ind != len(LR2[0]):
                ans = min(ans, (LR2[0][ind]-LR2[1][k]) * 10)


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

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]):
            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]):
            t.append((int(j[1])-int(i[1]))*5)
            
if len(t)==0:
    print("SAFE")
else:
    print(min(t))