結果

問題 No.3154 convex polygon judge
ユーザー nasutarou1341
提出日時 2025-05-20 21:36:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 992 ms / 2,000 ms
コード長 2,037 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 108,672 KB
最終ジャッジ日時 2025-05-20 21:36:17
合計ジャッジ時間 7,677 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
def det(p, q):
    return p[0]*q[1] - p[1]*q[0]
def sub(p, q):
    return (p[0]-q[0], p[1]-q[1])
def get_convex_hull(points):
    # どの3点も直線上に並んでいないと仮定する。
    n = len(points)
    points.sort()
    size_convex_hull = 0
    ch = []
    for i in range(n):
        while size_convex_hull > 1:
            v_cur = sub(ch[-1], ch[-2])
            v_new = sub(points[i], ch[-2])
            if det(v_cur, v_new) > 0:
                break
            size_convex_hull -= 1
            ch.pop()
        ch.append(points[i])
        size_convex_hull += 1
    t = size_convex_hull
    for i in range(n-2, -1, -1):
        while size_convex_hull > t:
            v_cur = sub(ch[-1], ch[-2])
            v_new = sub(points[i], ch[-2])
            if det(v_cur, v_new) > 0:
                break
            size_convex_hull -= 1
            ch.pop()
        ch.append(points[i])
        size_convex_hull += 1
    return ch[:-1]

# 多角形の面積 * 2 (2をかけると必ず整数になる)
def area(points):
  ans = 0
  N = len(points)
  x0, y0 = points[N - 1]
  for i in range(N):
    x1, y1 = points[i]
    ans += (x0 - x1) * (y0 + y1)
    x0, y0 = x1, y1
  return abs(ans)

# 多角形の線分上の格子点の数の和
def all_points_ontotu(points):
  ans = 0
  N = len(points)
  x0, y0 = points[N - 1]
  for i in range(N):
    x1, y1 = points[i]
    ans += points_online(x0, y0, x1, y1) - 1
    x0, y0 = x1, y1
  return ans

# 線分上の格子点の数
def points_online(x0, y0, x1, y1):
  return math.gcd(abs(x0 - x1), abs(y0 - y1)) + 1

# 凸包に含まれる格子点の数
def inner_points(points):
  D = get_convex_hull(points)
  S2 = area(D)
  b = all_points_ontotu(D)
  # ピックの定理で凸包に含まれる格子点の数を求める
  ans = (S2 - b) // 2 + 1
  ans += b # (辺上の格子点を含める)
  return ans

N = int(input())
XY = [list(map(int, input().split())) for _ in range(N)]

c = get_convex_hull(XY)
if len(c) == N:
  print("Yes")
else:
  print("No")
0