Guest User

hackerrank | polygon

a guest
Mar 17th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. N,Q = input().split()
  2. N,Q = int(N),int(Q)
  3. Np = [0]*N
  4. for i in range(N):
  5.     Np[i] = input().split()
  6.     for j in range(2):
  7.         Np[i][j] = int(Np[i][j])
  8.  
  9. def distance_sqr(a,b):
  10.     return (a[0]-b[0])**2 + (a[1]-b[1])**2
  11.  
  12. def d_distance_sqr (a,c):
  13.     return max(distance_sqr(a[0],c), distance_sqr(a[1],c))
  14.  
  15. def closest_vector_pair(ar, N):
  16.     d = 2**16
  17.     a = 0
  18.     for i in range(len(ar)):
  19.         if d > d_distance_sqr([ar[i-1],ar[i]], N):
  20.             d = d_distance_sqr([ar[i-1],ar[i]], N)
  21.             a = i
  22.     return [ar[a-1],ar[a]]
  23.  
  24. def scalar_prod(a, b):
  25.     return a[0]*b[0] + a[1]*b[1]
  26.  
  27. def is_dot_inside(ar, N):
  28.     t = 0
  29.     c = closest_vector_pair(ar, N)
  30.     if c[1][0] - c[0][0] == 0: #dx = 0
  31.         if c[1][1] - c[0][1] > 0: #dy > 0
  32.             if (N[0] - c[1][0]) >= 0:
  33.                 t=1
  34.             else:
  35.                 t=0
  36.         elif c[1][1] - c[0][1] < 0: #dy < 0
  37.             if (N[0] - c[1][0]) <= 0:
  38.                 t=1
  39.             else:
  40.                 t=0
  41.     elif c[1][1] - c[0][1] == 0: #dy = 0
  42.         if c[1][0] - c[0][0] > 0: #dx > 0
  43.             if N[1] - c[1][1] <= 0:
  44.                 t=1
  45.             else:
  46.                 t=0
  47.         elif c[1][0] - c[0][0] < 0: #dx < 0
  48.             if N[1] - c[1][1] >= 0:
  49.                 t=1
  50.             else:
  51.                 t=0
  52.     return t
  53.  
  54. for i in range(Q):        
  55.     temp = 0
  56.     M = int(input())
  57.     Mi = [0]*M
  58.     for k in range(M):
  59.         Mi[k] = input().split()
  60.         for j in range(2):
  61.             Mi[k][j] = int(Mi[k][j])
  62.  
  63.     for j in range(N):
  64.         temp += int(is_dot_inside(Mi, Np[j]))
  65.     print(temp)
Add Comment
Please, Sign In to add comment