Advertisement
Void-voiD

Untitled

Mar 6th, 2023
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | Source Code | 0 0
  1. def distance(x):
  2.     return (x[0] ** 2 + x[1] ** 2) ** 0.5
  3.  
  4.  
  5. def merge(A, B):
  6.     c = []
  7.     i = 0
  8.     j = 0
  9.     while i < len(A) and j < len(B):
  10.         if distance(B[j]) > distance(A[i]):
  11.             c.append(A[i])
  12.             i += 1
  13.         else:
  14.             c.append(B[j])
  15.             j += 1
  16.     c += A[i:] + B[j:]
  17.     return c
  18.  
  19.  
  20. def mergesort(A):
  21.     if len(A) < 2:
  22.         return A
  23.     m = len(A) // 2
  24.     L = mergesort(A[:m])
  25.     R = mergesort(A[m:])
  26.     Z = merge(L, R)
  27.     return Z
  28.  
  29.  
  30. n = int(input())
  31. a = [[]] * n
  32. for i in range(n):
  33.     a[i] = list(map(int, input().split()))
  34. ans = mergesort(a)
  35. for x in ans:
  36.     print(*x)
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement