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:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
point_dict = {}
time = 200000 * 100
for i in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == "U":
        for i in range(1, 200001):
            if y + i > 200000:
                break
            key = (x, y + i, i * 10)
            if key in point_dict:
                if i * 10 < time:
                    time = i * 10
            else:
                point_dict[key] = 1
    if u == "D":
        for i in range(1, 200001):
            if y - i < 0:
                break
            key = (x, y - i, i * 10)
            if key in point_dict:
                if i * 10 < time:
                    time = i * 10
            else:
                point_dict[key] = 1
    if u == "R":
        for i in range(1, 200001):
            if x + i > 200000:
                break
            key = (x + i, y, i * 10)
            if key in point_dict:
                if i * 10 < time:
                    time = i * 10
            else:
                point_dict[key] = 1
    if u == "L":
        for i in range(1, 200001):
            if x - i < 0:
                break
            key = (x - i, y, i * 10)
            if key in point_dict:
                if i * 10 < time:
                    time = i * 10
            else:
                point_dict[key] = 1

if time == 200000 * 100:
    print("SAFE")
else:
    print(time)

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):
    x[i],y[i],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(x[i])
    y[i]= int(y[i])
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,(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,(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,(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:
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):
    if plane[k][0] == plane[k - 1][0]:
      if plane[k][1] > plane[k - 1][1]:
        if (plane[k][4] == 1) and (plane[k - 1][4] == 0):
          time2 = (plane[k][1] - plane[k - 1][1]) / 0.2
          time = min(time, time2)
      else:
        if (plane[k][4] == 0) and (plane[k - 1][4] == 1):
          time2 = (plane[k - 1][1] - plane[k][1]) / 0.2
          time = min(time, time2)
        
#y座標が同じでぶつかる
plane = sorted(plane, key=lambda x:x[1])
for k in range(1, N):
    if plane[k][1] == plane[k - 1][1]:
      if plane[k][0] > plane[k - 1][0]:
        if (plane[k][4] == 3) and (plane[k - 1][4] == 2):
          time2 = (plane[k][0] - plane[k - 1][0]) / 0.2
          time = min(time, time2)
      else:
        if (plane[k][4] == 2) and (plane[k - 1][4] == 3):
          time2 = (plane[k - 1][0] - plane[k][0]) / 0.2
          time = min(time, time2)

def go(x, y, u, t):
  if u == 0:
    return x, int(y + t * 0.1)
  elif u== 1:
    return x, int(y - t * 0.1)
  elif u == 2:
    return int(x + t * 0.1), y
  else:
    return int(x - t * 0.1), y
  
          
#和が同じでぶつかる
plane = sorted(plane, key=lambda x:x[2])
for k in range(1, N):
  one = plane[k]
  j = 1
  T = True
  while T:
    if k - j >= 0:
      two = plane[k - j]
      if one[2] == two[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)
              T = False
      else:
        T = False
      j += 1
    else:
      T = False
    
#差が同じでぶつかる
plane = sorted(plane, key=lambda x:x[3])
for k in range(1, N):
  one = plane[k]
  j = 1
  T = True
  while T:
    if k - j >= 0:
      two = plane[k - j]
      if one[3] == two[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)
              T = False
      else:
        T = False
      j += 1
    else:
      T = False
      
if time == 10 ** 10:
  print("SAFE")
else:
  print(int(time))
#print(plane)

Submission status: Wrong Answer

Solution code:
n=int(input())
p=[list(input().split()) for _ in range(n)]
count=0
time=0
#print(p)
for i in range(n):
    p[i][0]=int(p[i][0])*10
    p[i][1]=int(p[i][1])*10
#print(p)
def forward(s):
    if s=='U':
        return list([0,1])
    elif s=='R':
        return list([1,0])
    elif s=='D':
        return list([0,-1])
    else:
        return list([-1,0])
        

while count==0:
    time+=1
    for i in range(n):
    #    print(forward(p[i][2]))
        p[i][0]=p[i][0]+forward(p[i][2])[0]
        p[i][1]=p[i][1]+forward(p[i][2])[1]
    #print(time)
    #print([str(int(p[i][0]))+str(int(p[i][1])) for i in range(n)])
    #print(len(set([str(int(p[i][0]))+str(int(p[i][1])) for i in range(n)])))
    if len(set([str(int(p[i][0]))+str(int(p[i][1])) for i in range(n)]))!=n:
        print(time)
        break
    if time==10**5:
        print('SAFE')
        break

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    yx_p_u = {}
    yx_p_r = {}
    yx_p_d = {}
    yx_p_l = {}

    yx_m_u = {}
    yx_m_r = {}
    yx_m_d = {}
    yx_m_l = {}

    for i in range(-200000,200000+1):
        yx_p_u[i] = []
        yx_p_r[i] = []
        yx_p_d[i] = []
        yx_p_l[i] = []

    for i in range(0,400000+1):
        yx_m_u[i] = []
        yx_m_r[i] = []
        yx_m_d[i] = []
        yx_m_l[i] = []


    for _ in range(n):
        x,y,u = map(str, input().split())
        x = int(x)
        y = int(y)
        yxp = y-x
        yxm = y+x
        if u == 'U':
            yx_p_u[yxp].append(x)
            yx_m_u[yxm].append(x)
        if u == 'R':
            yx_p_r[yxp].append(x)
            yx_m_r[yxm].append(x)
        if u == 'D':
            yx_p_d[yxp].append(x)
            yx_m_d[yxm].append(x)
        if u == 'L':
            yx_p_l[yxp].append(x)
            yx_m_l[yxm].append(x)


    # for i in range(-20,20+1):
    for i in range(-200000,200000+1):
        yx_p_u[i].sort()
        yx_p_r[i].sort()
        yx_p_d[i].sort()
        yx_p_l[i].sort()
        # if len(yx_p_u[i]) > 0: print(i,yx_p_u[i])
        # if len(yx_p_r[i])> 0: print(i,yx_p_r[i])
        # if len(yx_p_d[i])> 0: print(i,yx_p_d[i])
        # if len(yx_p_l[i])> 0: print(i,yx_p_l[i])


    for i in range(0,400000+1):
        yx_m_u[i].sort()
        yx_m_r[i].sort()
        yx_m_d[i].sort()
        yx_m_l[i].sort()
        # if yx_m_u[i]: print(i,yx_m_u)
        # if yx_m_r[i]: print(i,yx_m_u)
        # if yx_m_d[i]: print(i,yx_m_u)
        # if yx_m_l[i]: print(i,yx_m_u)


    ans = 10**9
    for i in range(-200000,200000+1):
        for v in yx_p_u[i]:
            ind = bisect_right(yx_p_l[i], v)
            ind = ind if 0 <= ind < len(yx_p_l[i]) else None
            val = yx_p_l[i][ind] if ind is not None else None
            if val is not None:
                ans = min(ans, abs(val-v)*10)

        for v in yx_p_d[i]:
            ind = bisect_left(yx_p_r[i], v) - 1
            ind = ind if 0 <= ind < len(yx_p_r[i]) else None
            val = yx_p_r[i][ind] if ind is not None else None
            if val is not None:
                ans = min(ans, abs(val-v)*10)


    for i in range(0,400000+1):
        for v in yx_m_d[i]:
            ind = bisect_right(yx_m_l[i], v)
            ind = ind if 0 <= ind < len(yx_m_l[i]) else None
            val = yx_m_l[i][ind] if ind is not None else None
            if val is not None:
                ans = min(ans, abs(val-v)*10)

        for v in yx_m_u[i]:
            ind = bisect_left(yx_m_r[i], v) - 1
            ind = ind if 0 <= ind < len(yx_m_r[i]) else None
            val = yx_m_r[i][ind] if ind is not None else None
            if val is not None:
                ans = min(ans, abs(val-v)*10)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

U = []
R = []
D = []
L = []

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

ans = float('inf')
for ux, uy in U:
    for dx, dy in D:
        if ux == dx and uy <= dy:
            ans = min(ans, ((dy-uy)//2)*10)
    for lx, ly in L:
        if lx - ux == ly - uy and lx - ux >= 0:
            ans = min(ans, (lx-ux) * 10)
    for rx, ry in R:
        if ux - rx == ry - uy and ux - rx >= 0:
            ans = min(ans, (rx-ux) * 10)

for dx, dy in D:
    for lx, ly in L:
        if lx - dx == dy - ly and lx - dx >= 0:
            ans = min(ans, (lx-dx) * 10)
    for rx, ry in R:
        if dx - rx == dy - ry and dx - rx >= 0:
            ans = min(ans, (rx-dx) * 10)

for lx, ly in L:
    for rx, ry in R:
        if ly == ry and lx >= rx:
            ans = min(ans, ((lx-rx)//2)*10)

if ans == float('inf'):
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
sky_mapping = {}
plane_l = []
min_x, min_y = 10**6, 10**6
max_x, max_y = 0, 0
dir_dic = {'U':(0, 1), 'D':(0, -1), 'L':(-1, 0), 'R':(1, 0)}
for i in range(N):
    x, y, u = input().split()
    plane_l.append(((int(x), int(y)), u))
    max_x = max(max_x, int(x))
    min_x = min(min_x, int(x))
    max_y = max(max_y, int(y))
    min_y = min(min_y, int(y))
result = []
for plane in plane_l:
    pos, dr = plane
    x, y = pos
    if dr == 'U':
        for i in range(max_y - y + 2):
            sky_mapping.setdefault((x, y+i), set())
            if i not in sky_mapping[(x, y+i)]:
                sky_mapping[(x, y+i)].add(i)
            else:
                result.append(i)
    if dr == 'D':
        for i in range(y - min_y + 2):
            sky_mapping.setdefault((x, y-i), set())
            if i not in sky_mapping[(x, y-i)]:
                sky_mapping[(x, y-i)].add(i)
            else:
                result.append(i)
    if dr == 'R':
        for i in range(max_x - x + 2):
            sky_mapping.setdefault((x+i, y), set())
            if i not in sky_mapping[(x+i, y)]:
                sky_mapping[(x+i, y)].add(i)
            else:
                result.append(i)
    if dr == 'L':
        for i in range(x - min_x + 2):
            sky_mapping.setdefault((x-i, y), set())
            if i not in sky_mapping[(x-i, y)]:
                sky_mapping[(x-i, y)].add(i)
            else:
                result.append(i)
if result == []:
    print('SAFE')
else:
    print(min(result)*10)

Submission status: Wrong Answer

Solution code:
n = int(input())
big = 800001
sml = 400000
pp = [[[] for _ in range(big)] for _ in range(8)]
mm = [[[] for _ in range(sml//2+1)] for _ in range(4)]
for _ in range(n):
  x,y,d = input().split()
  x = int(x)
  y = int(y)
  if d == 'R':
    pp[0][+x+y].append(+y)
    pp[2][+x-y].append(-y)
    mm[0][y].append(x)
  if d == 'L':
    pp[4][-x+y].append(+y)
    pp[6][-x-y].append(-y)
    mm[1][y].append(x)
  if d == 'U':
    pp[1][+x+y].append(+x)
    pp[5][-x+y].append(-x)
    mm[2][x].append(y)
  if d == 'D':
    pp[3][+x-y].append(+x)
    pp[7][-x-y].append(-x)
    mm[3][x].append(y)
import bisect
inf = 10**10
ans = inf
def ser(lx,ly,su):
  global ans
  lly = len(ly)
  for xx in lx:
    b = bisect.bisect_left(ly,su-xx)
    if b != lly:
      ans = min(ans,(xx+ly[b]-su)*10)
    else:
      pass
for d in range(4):
  xd = 2*d
  yd = 2*d+1
  for sun in range(-big+1,big):
    if pp[xd][sun] and pp[yd][sun]:
      #print(pp[xd][sun],pp[yd][sun],sun,xd,yd)
      ser(pp[xd][sun],pp[yd][sun],sun)
for sun in range(0,sml//2+1):
  if mm[0][sun] and mm[1][sun]:
    llm = len(mm[1][sun])
    for xx in mm[0][sun]:
      b = bisect.bisect_left(mm[1][sun],xx)
      if b != llm:
        ans = min(ans,(mm[1][sun][b]-xx)*5)
      else:
        pass
for sun in range(0,sml//2+1):
  if mm[2][sun] and mm[3][sun]:
    llm = len(mm[3][sun])
    for yy in mm[2][sun]:
      b = bisect.bisect_left(mm[3][sun],yy)
      if b != llm:
        ans = min(ans,(mm[3][sun][b]-yy)*5)
      else:
        pass
#print(ans)
if ans == inf:
  print('SAFE')
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
XYU = []
from collections import defaultdict, deque
UR = defaultdict(deque)
UL = defaultdict(deque)
DR = defaultdict(deque)
DL = defaultdict(deque)
LR = defaultdict(deque)
UD = defaultdict(deque)
for _ in range(N):
    x, y, u = input().split()
    x, y = int(x), int(y)
    if u=='U':
        UR[(x+y)].append((x, y))
        UL[(x-y)].append((x, y))
        UD[x].append((x, y))
    if u=='R':
        LR[y].append((x, y))
    if u=='D':
        DL[(x+y)].append((x, y))
        DR[(x-y)].append((x, y))
    XYU.append((x, y, u))

ans = 10**18
for x, y, u in XYU:
    if u=='R':
        for px, py in UR[(x+y)]:
            if x<px and y>py:
                ans = min(ans, px-x)
                
        for px, py in DR[(x-y)]:
            if x<px and py>y:
                ans = min(ans, px-x)
                
    if u=='L':
        for px, py in UL[(x-y)]:
            if x>px and y>py:
                ans = min(ans, x-px)
                
        for px, py in DL[(x+y)]:
            if x>px and py>y:
                ans = min(ans, x-px) 
                
        for px, py in LR[y]:
            if x>px:
                ans = min(ans, (x-px)/2)
                
    if u=='D':
        for px, py in UD[x]:
            if py<y:
                ans = min(ans, (y-py)/2)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_right
N = int(input())
XYU = [None]*N
for i in range(N):
    x, y, u = input().split()
    XYU[i] = (int(x), int(y), u)
U, D = defaultdict(list), defaultdict(list)
R, L = defaultdict(list), defaultdict(list) 
Ru, Rd, Lu, Ld = defaultdict(list), defaultdict(list), defaultdict(list), defaultdict(list)
for x, y, u in XYU:
    if u == 'R':
        Ru[x+y].append(x)
        Rd[y-x].append(x)
        R[y].append(x)
    elif u == 'L':
        Lu[y-x].append(x)
        Ld[x+y].append(x)
        L[y].append(x)
    elif u == 'U':
        U[x].append(y)
    else:
        D[x].append(y)
ans = float('inf')
for x in U:
    if len(D[x]) > 0:
        D[x].sort()
        le = len(D[x])
        for y in U[x]:
            idx = bisect_right(D[x], y)
            if idx < le and ans > (D[x][idx]-y)/2:
                ans = (D[x][idx]-y)/2
for y in R:
    if len(L[y]) > 0:
        L[y].sort()
        le = len(L[y])
        for x in R[y]:
            idx = bisect_right(L[y], x)
            if idx < le and ans > (L[y][idx]-x)/2:
                ans = (L[y][idx]-x)/2
for k in Ru:
    Ru[k].sort()
for k in Lu:
    Lu[k].sort()
for k in Rd:
    Rd[k].sort()
for k in Ld:
    Ld[k].sort()
for x, y, u in XYU:
    if u == 'U':
        if len(Ru[x+y]) > 0:
            idx = bisect_right(Ru[x+y], x)-1
            if 0 <= idx < len(Ru[x+y]):
                ans = min(ans, x-Ru[x+y][idx])
        if len(Lu[y-x]) > 0:
            idx = bisect_right(Ru[x+y], x)
            if idx < len(Lu[x+y]):
                ans = min(ans, Lu[x+y][idx]-x)
    elif u == 'D':
        if len(Rd[y-x]) > 0:
            idx = bisect_right(Rd[y-x], x)-1
            if 0 <= idx < len(Rd[y-x]):
                ans = min(ans, x-Rd[y-x][idx])
        if len(Ld[x+y]) > 0:
            idx = bisect_right(Ld[x+y], x)
            if idx < len(Ld[x+y]):
                ans = min(ans, Ld[x+y][idx]-x)
if ans == float('inf'):
    print('SAFE')
else:
    print(int(ans*10))

Submission status: Wrong Answer

Solution code:
N = int(input())

U = []
R = []
D = []
L = []

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

ans = float('inf')
for ux, uy in U:
    for dx, dy in D:
        if ux == dx and uy <= dy:
            ans = min(ans, ((dy-uy)//2)*10)
    for lx, ly in L:
        if lx - ux == ly - uy and lx - ux >= 0:
            ans = min(ans, (lx-ux) * 10)
    for rx, ry in R:
        if ux - rx == ry - uy and ux - rx >= 0:
            ans = min(ans, (rx-ux) * 10)

for dx, dy in D:
    for lx, ly in L:
        if lx - dx == dy - ly and lx - dx >= 0:
            ans = min(ans, (lx-dx) * 10)
    for rx, ry in R:
        if rx - dx == dy - ry and rx - dx >= 0:
            ans = min(ans, (rx-dx) * 10)

for lx, ly in L:
    for rx, ry in R:
        if ly == ry and lx >= rx:
            ans = min(ans, ((lx-rx)//2)*10)

if ans == float('inf'):
    print('SAFE')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
XYU = []
for _ in range(N):
    line = list(input().split())
    line[0] = int(line[0])
    line[1] = int(line[1])
    XYU.append(line)
# print(XYU)


def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]

for time in range(1, 2 * (10**5) + 1):
    xy = []
    for i in XYU:
        if i[2] == "R":
            i[0] += 1
        elif i[2] == "U":
            i[1] += 1
        elif i[2] == "L":
            i[0] -= 1
        elif i[2] == "D":
            i[1] -= 1

        xy.append(i[0:2])

    if len(get_unique_list(xy)) != N:
        print(time * 10)
        exit()

print("SAFE")

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
u=[]
r=[]
d=[]
l=[]
ans=float("inf")
for i in range(n):
    x,y,b=map(str,input().split())
    x,y=int(x),int(y)
    if b=="U":
        u.append((x,y))
    elif b=="R":
        r.append((x,y))
    elif b=="D":
        d.append((x,y))
    elif b=="L":
        l.append((x,y))
for i in u:
    x,y=i[0],i[1]
    for j in r:
        x1,y1=j[0],j[1]
        if x1>x and y<y1 and y1-y==x1-x:
            if 10*(x1-x)<ans:
                ans=10*(x1-x)
    for k in d:
        x2,y2=k[0],k[1]
        if y<y2 and x==x2:
            if 5*(y2-y)<ans:
                ans=5*(y2-y)
    for ii in l:
        x3,y3=ii[0],ii[1]
        if y<y3 and x<x3 and x3-x ==y3-y:
            if 10*(x3-x)<ans:
                ans=10*(x3-x)
for i in r:
    x,y=i[0],i[1]
    for j in d:
        x1,y1=j[0],j[1]
        if x<x1 and y<y1 and y1-y==x1-x:
            if 10*(y1-y)<ans:
                ans=10*(y1-y)
    for k in l:
        x2,y2=k[0],k[1]
        if y==y2 and x<x2:
            if 5*(x2-x)<ans:
                ans=5*(x2-x)
for i in d:
    x,y=i[0],i[1]
    for j in l:
        x1,y1=j[0],j[1]
        if x<x1 and y>y1 and x1-x==y-y1:
            if 10*(x1-x)<ans:
                ans=10*(x1-x)
                
if ans==float("inf"):
    print("SAFE")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
b = 2000001
x = []
u = []
r = []
d = []
l = []
for i in range(n):
    A = list(input().split())
    a = [int(A[0]), int(A[1])]
    if A[2] == "U":
        u.append(a)
    elif A[2] == "R":
        r.append(a)
    elif A[2] == "D":
        d.append(a)
    else:
        l.append(a)
#ud
u1 = {}
d1 = {}
for i in range(len(u)):
    u1[u[i][0]] = u[i][1]
for i in range(len(d)):
    d1[d[i][0]] = d[i][1]
for i in range(200001):
    if i in u1 and i in d1:
        if u1[i] < d1[i]:
            b = min(b, (d1[i]-u1[i])*5)
#rl
r1 = {}
l1 = {}
for i in range(len(r)):
    r1[r[i][1]] = r[i][0]
for i in range(len(l)):
    l1[l[i][1]] = l[i][0]
for i in range(200001):
    if i in r1 and i in l1:
        if r1[i] < l1[i]:
            b = min(b, (l1[i]-r1[i])*5)
#ur
u2 = {}
r2 = {}
for i in range(len(u)):
    u2[u[i][0]+u[i][1]] = u[i][0]
for i in range(len(r)):
    r2[r[i][0]+r[i][1]] = r[i][0]
for i in range(400001):
    if i in u2 and i in r2:
        if r2[i] < u2[i]:
            b = min(b, (u2[i]-r2[i])*10)
#ld
l2 = {}
d2 = {}
for i in range(len(l)):
    l2[l[i][0]+l[i][1]] = l[i][0]
for i in range(len(d)):
    d2[d[i][0]+d[i][1]] = d[i][0]
for i in range(400001):
    if i in l2 and i in d2:
        if d2[i] < l2[i]:
            b = min(b, (l2[i]-d2[i])*10)
#lu
l3 = {}
u3 = {}
for i in range(len(l)):
    l3[l[i][0]-l[i][1]] = l[i][0]
for i in range(len(u)):
    u3[u[i][0]-u[i][1]] = u[i][0]
for i in range(-200000, 2000001):
    if i in l3 and i in u3:
        if u3[i] < l3[i]:
            b = min(b, (l3[i]-u3[i])*10)
#dr
d3 = {}
r3 = {}
for i in range(len(d)):
    d3[d[i][0]-d[i][1]] = d[i][0]
for i in range(len(r)):
    r3[r[i][0]-r[i][1]] = r[i][0]
for i in range(-200000, 2000001):
    if i in d3 and i in r3:
        if r3[i] < d3[i]:
            b = min(b, (d3[i]-r3[i])*10)
if b == 2000001:
    print("SAFE")
else:
    print(b)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
XYU = []
from collections import defaultdict, deque
UR = defaultdict(deque)
UL = defaultdict(deque)
DR = defaultdict(deque)
DL = defaultdict(deque)
LR = defaultdict(deque)
UD = defaultdict(deque)
for _ in range(N):
    x, y, u = input().split()
    x, y = int(x), int(y)
    if u=='U':
        UR[(x+y)].append((x, y))
        UL[(x-y)].append((x, y))
        UD[x].append((x, y))
    if u=='R':
        LR[y].append((x, y))
    if u=='D':
        DL[(x+y)].append((x, y))
        DR[(x-y)].append((x, y))
    XYU.append((x, y, u))

ans = 10**18
for x, y, u in XYU:
    if u=='R':
        for px, py in UR[(x+y)]:
            if x<px and y>py:
                ans = min(ans, px-x)
                
        for px, py in DR[(x-y)]:
            if x<px and py>y:
                ans = min(ans, px-x)
                
    if u=='L':
        for px, py in UL[(x-y)]:
            if x>px and y>py:
                ans = min(ans, x-px)
                
        for px, py in DL[(x+y)]:
            if x>px and py>y:
                ans = min(ans, x-px) 
                
        for px, py in LR[y]:
            if x>px:
                ans = min(ans, (x-px)//2)
                
    if u=='D':
        for px, py in UD[x]:
            if py<y:
                ans = min(ans, (y-py)//2)

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

Submission status: Wrong Answer

Solution code:
from sys import exit

(n,),*xyu = [s.split() for s in open(0)]
n = int(n)

dxy = {
    'U':[0,1],
    'D':[0,-1],
    'R':[1,0],
    'L':[-1,0],
}

drs = set()
INF = float('inf')

for i in range(n):
    x,y,u = xyu[i]
    # xmin xmax ymin ymax
    drs.add(u)
    xyu[i] = ([int(x),int(y),*dxy[u],u])

if len(drs)==1:
    print('SAFE')
    exit()

time = 0

while 1:
    pos = set()
    rngs = dict.fromkeys(drs)
    time += 1
    for i in range(n):
        x,y,dx,dy,u = xyu[i]
        x += dx
        y += dy
        if (x,y) in pos:
            print(time*10)
            exit()
        else:
            pos.add((x,y))
        if rngs[u] is not None:
            xmin,xmax,ymin,ymax = rngs[u]
            rngs[u] = (min(xmin,x),max(xmax,x),min(ymin,y),max(ymax,y))
        else:
            rngs[u] = x,x,y,y
        xyu[i][0] = x
        xyu[i][1] = y

    safe = True
    if 'U' in drs:
        tmp = -INF
        for dr in drs-{'U'}:
            tmp = max(tmp, rngs[dr][3])
#         print(tmp)
        if tmp > rngs['U'][2]:
#             print('f1')
            safe = False
    if 'D' in drs:
        tmp = INF
        for dr in drs-{'D'}:
            tmp = min(tmp, rngs[dr][2])
#         print(tmp)
        if tmp < rngs['D'][3]:
#             print('f2')
            safe = False

    if 'R' in drs:
        tmp = -INF
        for dr in drs-{'R'}:
            tmp = max(tmp, rngs[dr][1])
#         print(tmp)
        if tmp > rngs['R'][0]:
#             print('f3')
            safe = False
    if 'L' in drs:
        tmp = INF
        for dr in drs-{'L'}:
            tmp = min(tmp, rngs[dr][0])
#         print(tmp)
        if tmp < rngs['L'][1]:
#             print('f4')
            safe = False
    if safe:
        print('SAFE')
        exit()

Submission status: Wrong Answer

Solution code:
N=int(input())
data=[list(input().split()) for i in range(N)]


gr=[]
for i in range(N):
    for j in range(N-i):
        if int(data[i][0])==int(data[j][0]):
            if int(data[i][1])>int(data[j][1]) and data[i][2]=="D" and data[j][2]=="U":
                count=(int(data[i][1])-int(data[j][1]))/0.2
                gr.append(count)
            elif int(data[i][1])<int(data[j][1]) and data[i][2]=="U" and data[j][2]=="D":
                count=(int(data[j][1])-int(data[i][1]))/0.2
                gr.append(count)

        if int(data[i][1])==int(data[j][1]):
            if int(data[i][0])>int(data[j][0]) and data[i][2]=="L" and data[j][2]=="R":
                count=(int(data[i][0])-int(data[j][0]))/0.2
                gr.append(count)
            elif int(data[i][0])<int(data[j][0]) and data[i][2]=="R" and data[j][2]=="L":
                count=(int(data[j][0])-int(data[i][0]))/0.2
                gr.append(count)


if len(gr)!=0:
    print(int(min(gr)))
else:
    print("SAFE")

Submission status: Wrong Answer

Solution code:
import itertools
from math import ceil

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 = abs(one[0] - two[0])
            if(dif % 2 == 0):
                ans = min(ans,dif//2*10)
            
    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 = abs(one[1] - two[1])
            if(dif % 2 == 0):
                ans = min(ans,dif//2 * 10)

    prd = list(itertools.product(u,r))
    for j in prd:
        one = j[0]
        two = j[1]
        if(one[0]-two[1] == two[0]-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] == two[1]-two[0] 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] == two[0]-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(one[0]-two[1] == two[0]-one[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()