fork download
  1. # print("hello python world")
  2.  
  3. # 블록 껍질
  4. '''https://w...content-available-to-author-only...c.net/problem/1708'''
  5.  
  6. # Method : 그라함 스캔 알고리즘
  7.  
  8. import sys
  9.  
  10.  
  11. # 각도 구하는 알고리즘
  12. # 실제 각도가 아닌 dx가 0이 되는 것을 방지하기 위함
  13. def theta(a, b):
  14. xa, ya = points[a]
  15. xb, yb = points[b]
  16.  
  17. dx, dy = xb - xa, yb - ya
  18. ax, ay = abs(dx), abs(dy)
  19.  
  20. if ax + ay == 0:
  21. return 0
  22.  
  23. else:
  24. t = dy / (ax + ay)
  25.  
  26. if dx < 0:
  27. return 2 - t
  28. elif dy < 0:
  29. return 4 + t
  30. else:
  31. return t
  32.  
  33.  
  34. # CCW 알고리즘 : 벡터의 외적에서 방향을 확인
  35. def ccw(i, j, k):
  36. (vx1, vy1), (vx2, vy2) = vector(i, j), vector(j, k)
  37. product_value = vx1 * vy2 - vy1 * vx2
  38. return True if product_value > 0 else False
  39.  
  40.  
  41. # vector 생성 함수
  42. def vector(idx1, idx2): # 1 -> 2로 가는 벡터 생성
  43. (x1, y1), (x2, y2) = points[idx1], points[idx2]
  44. return x2 - x1, y2 - y1
  45.  
  46.  
  47. if __name__ == "__main__":
  48. try:
  49. N = int(sys.stdin.readline())
  50. points = [tuple(map(int, sys.stdin.readline().split(" "))) for _ in range(N)]
  51.  
  52. standard_point = 0
  53.  
  54. for idx, (x, y) in enumerate(points[1:], start=1):
  55. if points[standard_point][1] > y:
  56. standard_point = idx
  57. elif points[standard_point][1] == y and points[standard_point][0] < x:
  58. standard_point = idx
  59.  
  60. theta_list = [[i, 10] for i in range(N)]
  61.  
  62. for idx in range(N):
  63. theta_list[idx][1] = theta(standard_point, idx)
  64.  
  65. theta_list.sort(key=lambda x: x[1])
  66.  
  67. print("sorted : ")
  68. for p, _ in theta_list:
  69. print(points[p])
  70.  
  71. stack = [standard_point]
  72.  
  73. for p, _ in theta_list:
  74. if p == standard_point:
  75. continue
  76.  
  77. while len(stack) > 1 and not ccw(stack[-2], stack[-1], p):
  78. stack.pop()
  79.  
  80. stack.append(p)
  81.  
  82. while len(stack) > 1 and not ccw(stack[-2], stack[-1], standard_point):
  83. stack.pop()
  84.  
  85. print(len(stack))
  86. print("convex hull : ")
  87. for idx in stack:
  88. print(*points[idx])
  89.  
  90. except ValueError or IndexError as e:
  91. print(e)
Success #stdin #stdout 0.05s 9724KB
stdin
105
13 100
0 27
0 7
-13 13
-80 80
-85 85
-3 3
0 67
-58 58
-16 16
71 71
-67 67
89 89
0 0
0 84
64 64
0 24
0 55
43 43
68 68
-21 21
29 29
0 53
8 8
-19 19
97 97
-100 100
100 100
76 76
-23 23
63 63
-40 40
-98 98
-6 6
-92 92
-43 43
-31 31
0 41
61 61
99 99
0 15
25 25
0 38
0 89
62 62
66 66
-51 51
67 67
0 18
42 42
52 52
0 10
-57 57
0 58
56 56
-29 29
5 5
81 81
95 95
-36 36
70 70
88 88
-87 87
0 11
0 14
60 60
3 3
-46 46
0 94
0 44
77 77
2 2
16 16
0 92
83 83
-28 28
87 87
-10 10
30 30
-56 56
-86 86
-73 73
-20 20
0 63
21 21
92 92
10 10
98 98
0 37
-27 27
74 74
7 7
0 101
65 65
48 48
0 90
0 59
-69 69
0 25
-17 17
-63 63
14 14
0 57
-78 78
0 88
stdout
sorted : 
(0, 0)
(71, 71)
(89, 89)
(64, 64)
(43, 43)
(68, 68)
(29, 29)
(8, 8)
(97, 97)
(100, 100)
(76, 76)
(63, 63)
(61, 61)
(99, 99)
(25, 25)
(62, 62)
(66, 66)
(67, 67)
(42, 42)
(52, 52)
(56, 56)
(5, 5)
(81, 81)
(95, 95)
(70, 70)
(88, 88)
(60, 60)
(3, 3)
(77, 77)
(2, 2)
(16, 16)
(83, 83)
(87, 87)
(30, 30)
(21, 21)
(92, 92)
(10, 10)
(98, 98)
(74, 74)
(7, 7)
(65, 65)
(48, 48)
(14, 14)
(13, 100)
(0, 27)
(0, 7)
(0, 67)
(0, 84)
(0, 24)
(0, 55)
(0, 53)
(0, 41)
(0, 15)
(0, 38)
(0, 89)
(0, 18)
(0, 10)
(0, 58)
(0, 11)
(0, 14)
(0, 94)
(0, 44)
(0, 92)
(0, 63)
(0, 37)
(0, 101)
(0, 90)
(0, 59)
(0, 25)
(0, 57)
(0, 88)
(-13, 13)
(-80, 80)
(-85, 85)
(-3, 3)
(-58, 58)
(-16, 16)
(-67, 67)
(-21, 21)
(-19, 19)
(-100, 100)
(-23, 23)
(-40, 40)
(-98, 98)
(-6, 6)
(-92, 92)
(-43, 43)
(-31, 31)
(-51, 51)
(-57, 57)
(-29, 29)
(-36, 36)
(-87, 87)
(-46, 46)
(-28, 28)
(-10, 10)
(-56, 56)
(-86, 86)
(-73, 73)
(-20, 20)
(-27, 27)
(-69, 69)
(-17, 17)
(-63, 63)
(-78, 78)
5
convex hull : 
0 0
14 14
13 100
0 101
-100 100