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())

dic_up = {}
dic_down = {}
dic_hor = {}
dic_ver = {}
lst = []
for i in range(n):
    xi, yi, ui = input().split()
    xi, yi = int(xi), int(yi)
    up = xi - yi
    down = xi + yi
    if (ui, up) in dic_up:
        dic_up[(ui, up)].append([xi, yi])
    else:
        dic_up[(ui, up)] = [[xi, yi]]
    if (ui, down) in dic_down:
        dic_down[(ui, down)].append([xi, yi])
    else:
        dic_down[(ui, down)] = [[xi, yi]]
    if (ui, xi) in dic_ver:
        dic_ver[(ui, xi)].append(yi)
    else:
        dic_ver[(ui, xi)] = [yi]
    if (ui, yi) in dic_hor:
        dic_hor[(ui, yi)].append(xi)
    else:
        dic_hor[(ui, yi)] = [xi]
    lst.append([xi, yi, ui, up, down])

ans = 10**9
for i in range(n):
    xi, yi, ui, up, down = lst[i]
    if ui == 'U':
        if ('R', down) in dic_down:
            kouho = 10**9
            for rx, ry in dic_down[('R', down)]:
                if xi-rx > 0:
                    kouho = min(kouho, (xi-rx)*10)
            ans = min(ans, kouho)
        if ('D', xi) in dic_ver:
            kouho = 10**9
            for dy in dic_ver[('D', xi)]:
                kouho = 10**9
                if dy > yi:
                    kouho = min(kouho, (dy-yi)*5)
            ans = min(ans, kouho)
        if ('L', up) in dic_up:
            kouho = 10**9
            for lx, ly in dic_up[('L', up)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*10)
    if ui == 'R':
        if ('D', up) in dic_up:
            kouho = 10**9
            for dx, dy in dic_up[('D', up)]:
                if dx > xi:
                    kouho = min(kouho, (dx-xi)*10)
            ans = min(ans, kouho)
        if ('L', yi) in dic_hor:
            kouho = 10**9
            for lx in dic_hor[('L', yi)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*5)
            ans = min(ans, kouho)
    if ui == 'D':
        if ('L', down) in dic_down:
            kouho = 10**9
            for lx, ly in dic_down[('L', down)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*10)
            ans = min(ans, kouho)

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
def input(): return sys.stdin.readline().rstrip()


def culc(li):
    li.sort(key=lambda x: x[0])
    ans = 10**9
    for i in range(len(li)-1):
        if li[i][1] == '+' and li[i+1][1] == '-':
            ans = min(ans, li[i+1][0]-li[i][0])
    #print(ans,li)
    return ans


def main():
    N = int(input())
    axisX = defaultdict(list)
    axisY = defaultdict(list)
    XplusYplus = defaultdict(list)
    XminusYminus = defaultdict(list)
    XminusYplus = defaultdict(list)
    XplusYminus = defaultdict(list)
    ans = 10**9
    for _ in range(N):
        x, y, u = input().split()
        x, y = int(x), int(y)
        if u == 'U':
            axisY[x].append([y, '+'])
            XplusYplus[x+y].append([y-x, '+'])
            XminusYplus[x-y].append([x+y, '+'])
        elif u == 'R':
            axisX[y].append([x, '+'])
            XplusYplus[x+y].append([y-x, '-'])
            XplusYminus[x-y].append([x+y, '-'])
        elif u == 'D':
            axisY[x].append([y, '-'])
            XminusYminus[x+y].append([y-x, '-'])
            XplusYminus[x-y].append([x+y, '+'])
        elif u == 'L':
            axisX[y].append([x, '-'])
            XminusYminus[x+y].append([y-x, '+'])
            XminusYplus[x-y].append([x+y, '-'])

    for v in axisX.values():
        if len(v) > 1:
            ans = min(culc(v), ans)
    for v in axisY.values():
        if len(v) > 1:
            ans = min(culc(v), ans)
    for v in XplusYplus.values():
        if len(v) > 1:
            ans = min(culc(v), ans)
    for v in XplusYminus.values():
        if len(v) > 1:
            ans = min(culc(v), ans)
    for v in XminusYplus.values():
        if len(v) > 1:
            ans = min(culc(v), ans)
    for v in XminusYminus.values():
        if len(v) > 1:
            ans = min(culc(v), ans)
    print('SAFE' if ans > 10**8 else 5 * ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq

N = int(input())
X = [0]*N
Y = [0]*N
U = [0]*N
for i in range(N):
  x, y, u = input().split()
  X[i], Y[i], U[i] = int(x), int(y), u
min_collision = 2000010
  
# Y方向に衝突
hps = [[] for _ in range(200001)]

for i in range(N):
  if U[i]=='U':
    heapq.heappush(hps[X[i]], (Y[i], 1))
  elif U[i]=='D':
    heapq.heappush(hps[X[i]], (Y[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.2)    
    
# X方向に衝突
hps = [[] for _ in range(200001)]

for i in range(N):
  if U[i]=='R':
    heapq.heappush(hps[Y[i]], (X[i], 1))
  elif U[i]=='L':
    heapq.heappush(hps[Y[i]], (X[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.2)
    
# RとUが衝突
hps = [[] for _ in range(400001)]

for i in range(N):
  if U[i]=='R':
    heapq.heappush(hps[X[i]+Y[i]], (X[i], 1))
  elif U[i]=='U':
    heapq.heappush(hps[X[i]+Y[i]], (X[i], -1))

for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    #print(hp)
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.1)
    #print(temp_max_up, min_collision)
    
# DとLが衝突
hps = [[] for _ in range(400001)]

for i in range(N):
  if U[i]=='D':
    heapq.heappush(hps[X[i]+Y[i]], (X[i], 1))
  elif U[i]=='L':
    heapq.heappush(hps[X[i]+Y[i]], (X[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.1)
    
# UとLが衝突
hps = [[] for _ in range(400001)]

for i in range(N):
  if U[i]=='U':
    heapq.heappush(hps[X[i]-Y[i]+200000], (X[i], 1))
  elif U[i]=='L':
    heapq.heappush(hps[X[i]-Y[i]+200000], (X[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.1)
    
# RとDが衝突
hps = [[] for _ in range(400001)]

for i in range(N):
  if U[i]=='R':
    heapq.heappush(hps[X[i]-Y[i]+200000], (X[i], 1))
  elif U[i]=='D':
    heapq.heappush(hps[X[i]-Y[i]+200000], (X[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.1)
    
ans = int(min_collision)

if ans<2000010:
  print(ans)
else:
  print('SAFE')

Submission status: Wrong Answer

Solution code:
import heapq

N = int(input())
X = [0]*N
Y = [0]*N
U = [0]*N
for i in range(N):
  x, y, u = input().split()
  X[i], Y[i], U[i] = int(x), int(y), u
min_collision = 200000
  
# Y方向に衝突
hps = [[] for _ in range(200001)]

for i in range(N):
  if U[i]=='U':
    heapq.heappush(hps[X[i]], (Y[i], 1))
  elif U[i]=='D':
    heapq.heappush(hps[X[i]], (Y[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.2)    
    
# X方向に衝突
hps = [[] for _ in range(200001)]

for i in range(N):
  if U[i]=='R':
    heapq.heappush(hps[Y[i]], (X[i], 1))
  elif U[i]=='L':
    heapq.heappush(hps[Y[i]], (X[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.2)
    
# RとUが衝突
hps = [[] for _ in range(400001)]

for i in range(N):
  if U[i]=='R':
    heapq.heappush(hps[X[i]+Y[i]], (X[i], 1))
  elif U[i]=='U':
    heapq.heappush(hps[X[i]+Y[i]], (X[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.1)
    
# DとLが衝突
hps = [[] for _ in range(400001)]

for i in range(N):
  if U[i]=='D':
    heapq.heappush(hps[X[i]+Y[i]], (X[i], 1))
  elif U[i]=='L':
    heapq.heappush(hps[X[i]+Y[i]], (X[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.1)
    
# UとLが衝突
hps = [[] for _ in range(400001)]

for i in range(N):
  if U[i]=='U':
    heapq.heappush(hps[X[i]-Y[i]+200000], (X[i], 1))
  elif U[i]=='L':
    heapq.heappush(hps[X[i]-Y[i]+200000], (X[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.1)
    
# RとDが衝突
hps = [[] for _ in range(400001)]

for i in range(N):
  if U[i]=='R':
    heapq.heappush(hps[X[i]-Y[i]+200000], (X[i], 1))
  elif U[i]=='D':
    heapq.heappush(hps[X[i]-Y[i]+200000], (X[i], -1))
    
for hp in hps:
  temp_max_up = 200000
  for i in range(len(hp)):
    if hp[i][1]>0:
      temp_max_up = hp[i][0]
    else:
      if temp_max_up < 200000:
        min_collision = min(min_collision, (hp[i][0]-temp_max_up)/0.1)
    
ans = int(min_collision)

if ans<200000:
  print(ans)
else:
  print('SAFE')

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from collections import defaultdict

n = int(input())

xysame, upright, downright = {},{},{}
u_list = ("U","R","D","L")
for u in u_list:
    xysame[u] = defaultdict(list)
    upright[u] = defaultdict(list)
    downright[u] = defaultdict(list)

for _ in range(n):
    x,y,u = input().split()
    x,y = int(x),int(y)
    if u == "U" or "D":
        xysame[u][x].append(y)
    else:
        xysame[u][y].append(x)
    upright[u][y-x].append(x)
    downright[u][y+x].append(x)
    
for u in u_list:
    for key in xysame[u].keys():
        xysame[u][key].sort()
    for key in upright[u].keys():
        upright[u][key].sort()
    for key in downright[u].keys():
        downright[u][key].sort()

INF = 10**6
ans = INF


def line(low,high,type_ = 1):
    ret = INF
    for k,v in low.items():
        if high[k] == []: continue

        for vi in v:
            index = bisect_left(high[k], vi)
            if index == len(high[k]): continue
            ret = min(ret,(high[k][index]-vi)/type_)
    return ret

ans = min(ans, line(xysame["U"],xysame["D"],2))
ans = min(ans, line(xysame["R"],xysame["L"],2))
ans = min(ans, line(upright["R"],upright["D"],1))
ans = min(ans, line(upright["U"],upright["L"],1))
ans = min(ans, line(downright["R"],downright["U"],1))
ans = min(ans, line(downright["D"],downright["L"],1))

ans = int(ans*10)
if ans ==INF*10: ans = "SAFE"
    
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
from collections import defaultdict

n = int(input())

xysame, upright, downright = {},{},{}
u_list = ("U","R","D","L")
for u in u_list:
    xysame[u] = defaultdict(list)
    upright[u] = defaultdict(list)
    downright[u] = defaultdict(list)

for _ in range(n):
    x,y,u = input().split()
    x,y = int(x),int(y)
    if u == "U" or "D":
        xysame[u][x].append(y)
    else:
        xysame[u][y].append(x)
    upright[u][y-x].append(x)
    downright[u][y+x].append(x)
    
for u in u_list:
    for key in xysame[u].keys():
        xysame[u][key].sort()
    for key in upright[u].keys():
        upright[u][key].sort()
    for key in downright[u].keys():
        downright[u][key].sort()

INF = 10**6
ans = INF


def line(low,high,type_ = 1):
    ret = INF
    for k,v in low.items():
        if high[k] == []: continue

        for v1 in v:
            for v2 in high[k]:
                if v2-v1>0:
                    ret = min(ret,(v2-v1)/type_)
    return ret

ans = min(ans, line(xysame["U"],xysame["D"],2))
ans = min(ans, line(xysame["R"],xysame["L"],2))
ans = min(ans, line(upright["R"],upright["D"],1))
ans = min(ans, line(upright["U"],upright["L"],1))
ans = min(ans, line(downright["R"],downright["U"],1))
ans = min(ans, line(downright["D"],downright["L"],1))

ans = int(ans*10)
if ans ==INF*10: ans = "SAFE"
    
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        try:
            val = B_[bi]
        except:
            continue
        if val == asum:
            bi = Bsum[bi][1]
            x1, y1 = A[ai]
            x2, y2 = B[bi]
            time = (x2 - x1) * ope
            if time <= 0:
                continue
            ans = min(ans, time*10)
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        try:
            val = B_[bi]
        except:
            continue
        if val == asum:
            bi = Bsum[bi][1]
            x1, y1 = A[ai]
            x2, y2 = B[bi]
            if x1 == x2:
                time = y2 - y1
            else:
                time = x2 - x1
            if time <= 0:
                continue
            ans = min(ans, time*5)
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y, i) for i, (x,y) in enumerate(R)])
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y, i) for i, (x, y) in enumerate(L)])
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y, i) for i, (x, y) in enumerate(L)])
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y, i) for i, (x, y) in enumerate(R)])
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, i) for i, (x, y) in enumerate(D)])
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, i) for i, (x, y) in enumerate(L)])
    ans = calc_line(RR, LL, R, L, ans)

    if ans == INF:
        print('SAFE')
    else:
        print(int(ans))



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        try:
            val = B_[bi]
        except:
            continue
        if val == asum:
            bi = Bsum[bi][1]
            x1, y1 = A[ai]
            x2, y2 = B[bi]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        try:
            val = B_[bi]
        except:
            continue
        if val == asum:
            bi = Bsum[bi][1]
            x1, y1 = A[ai]
            x2, y2 = B[bi]
            if x1 == x2:
                time = y2 - y1
            else:
                time = x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y, i) for i, (x,y) in enumerate(R)])
    #ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y, i) for i, (x, y) in enumerate(L)])
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y, i) for i, (x, y) in enumerate(L)])
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y, i) for i, (x, y) in enumerate(R)])
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, i) for i, (x, y) in enumerate(D)])
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, i) for i, (x, y) in enumerate(L)])
    ans = calc_line(RR, LL, R, L, ans)

    if ans == INF:
        print('SAFE')
    else:
        print(int(ans))



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        try:
            val = B_[bi]
        except:
            continue
        if val == asum:
            bi = Bsum[bi][1]
            x1, y1 = A[ai]
            x2, y2 = B[bi]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time*10)
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        try:
            val = B_[bi]
        except:
            continue
        if val == asum:
            bi = Bsum[bi][1]
            x1, y1 = A[ai]
            x2, y2 = B[bi]
            if x1 == x2:
                time = y2 - y1
            else:
                time = x2 - x1
            if time < 0:
                continue
            ans = min(ans, time*5)
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y, i) for i, (x,y) in enumerate(R)])
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y, i) for i, (x, y) in enumerate(L)])
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y, i) for i, (x, y) in enumerate(L)])
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y, i) for i, (x, y) in enumerate(R)])
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, i) for i, (x, y) in enumerate(D)])
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, i) for i, (x, y) in enumerate(L)])
    ans = calc_line(RR, LL, R, L, ans)

    if ans == INF:
        print('SAFE')
    else:
        print(int(ans))



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def calc(Asum, Bsum, A, B, ans, ope = 1):
    if not len(A) or not len(B):
        return ans

    #print(Asum, Bsum)
    B_ = [val for val, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        try:
            val = B_[bi]
        except:
            continue
        if val == asum:
            bi = Bsum[bi][1]
            x1, y1 = A[ai]
            x2, y2 = B[bi]
            time = (x2 - x1) * ope
            if time < 0:
                continue
            ans = min(ans, time)
    return ans

def calc_line(Asum, Bsum, A, B, ans):
    if not len(A) or not len(B):
        return ans

    # print(Asum, Bsum)
    B_ = [val for val, _ in Bsum]
    for asum, ai in Asum:
        bi = bisect.bisect_left(B_, asum)
        try:
            val = B_[bi]
        except:
            continue
        if val == asum:
            bi = Bsum[bi][1]
            x1, y1 = A[ai]
            x2, y2 = B[bi]
            if x1 == x2:
                time = y2 - y1
            else:
                time = x2 - x1
            if time < 0:
                continue
            ans = min(ans, time/2)
    return ans

def run():
    U, D, R, L = [],[],[],[]
    N = int(sysread())
    for i in range(N):
        x,y,u = input().split()
        #print(x, y, u)
        x,y = int(x), int(y)
        if u == 'U':
            U.append((x,y))
        elif u == 'D':
            D.append((x,y))
        elif u == 'R':
            R.append((x,y))
        elif u == 'L':
            L.append((x,y))

    #print(U, D, R, L)
    ans = INF
    # U vs R
    Usum = sorted([(x+y, i) for i, (x,y) in enumerate(U)])
    Rsum = sorted([(x+y, i) for i, (x,y) in enumerate(R)])
    ans = calc(Usum, Rsum, U, R, ans, ope=-1)

    # D vs L
    Dsum = sorted([(x + y, i) for i, (x, y) in enumerate(D)])
    Lsum = sorted([(x + y, i) for i, (x, y) in enumerate(L)])
    ans = calc(Dsum, Lsum, D, L, ans, ope=1)

    # U vs L
    Usub = sorted([(x - y, i) for i, (x, y) in enumerate(U)])
    Lsub = sorted([(x - y, i) for i, (x, y) in enumerate(L)])
    ans = calc(Usub, Lsub, U, L, ans, ope=1)

    # D vs R
    Dsub = sorted([(x - y, i) for i, (x, y) in enumerate(D)])
    Rsub = sorted([(x - y, i) for i, (x, y) in enumerate(R)])
    ans = calc(Dsub, Rsub, D, R, ans, ope=-1)

    # U vs D
    UU = sorted([(x, i) for i, (x,y) in enumerate(U)])
    DD = sorted([(x, i) for i, (x, y) in enumerate(D)])
    ans = calc_line(UU, DD, U, D, ans)

    # R vs L
    RR = sorted([(y, i) for i, (x, y) in enumerate(R)])
    LL = sorted([(y, i) for i, (x, y) in enumerate(L)])
    ans = calc_line(RR, LL, R, L, ans)

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



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
N=int(input())
u,d,r,l = [],[],[],[]
for i in range(N):
    x,y,dir = input().split()
    if dir == 'U':
        u.append(tuple(map(int, (x,y))))
    if dir == 'D':
        d.append(tuple(map(int, (x,y))))
    if dir == 'R':
        r.append(tuple(map(int, (x,y))))
    if dir == 'L':
        l.append(tuple(map(int, (x,y))))

dmax = -1

for rx,ry in r:
    for lx,ly in l:
        if ry == ly:
            dmax = max(dmax, 5*(lx - rx))
    for ux,uy in u:
        x = ux - rx
        y = ry - uy
        if x == y:
            dmax = max(dmax, x*10)
    for dx,dy in d:
        x = dx - rx
        y = dy - ry
        if x == y:
            dmax = max(dmax, x*10)
for lx,ly in l:
    for ux,uy in u:
        x = lx - ux
        y = ly - uy
        if x == y:
            dmax = max(dmax, x*10)
    for dx,dy in d:
        x = lx - dx
        y = dy - ly
        if x == y:
            dmax = max(dmax, x*10)
for ux,uy in u:
    for dx,dy in d:
        if dx == ux:
            dmax = max(dmax, 5*(dy - uy))
print("SAFE" if dmax == -1 else dmax)

Submission status: Wrong Answer

Solution code:
N=int(input())
u,d,r,l = [],[],[],[]
for i in range(N):
    x,y,dir = input().split()
    if dir == 'U':
        u.append(tuple(map(int, (x,y))))
    if dir == 'D':
        d.append(tuple(map(int, (x,y))))
    if dir == 'R':
        r.append(tuple(map(int, (x,y))))
    if dir == 'L':
        l.append(tuple(map(int, (x,y))))

dmax = 0

for rx,ry in r:
    for lx,ly in l:
        if ry == ly:
            dmax = max(dmax, 5*(lx - rx))
    for ux,uy in u:
        x = ux - rx
        y = ry - uy
        if x == y:
            dmax = max(dmax, x*10)
    for dx,dy in d:
        x = dx - rx
        y = dy - ry
        if x == y:
            dmax = max(dmax, x*10)
for lx,ly in l:
    for ux,uy in u:
        x = lx - ux
        y = ly - uy
        if x == y:
            dmax = max(dmax, x*10)
    for dx,dy in d:
        x = lx - dx
        y = dy - ly
        if x == y:
            dmax = max(dmax, x*10)
for ux,uy in u:
    for dx,dy in d:
        if dx == ux:
            dmax = max(dmax, 5*(dy - uy))
print("SAFE" if dmax == 0 else dmax)