結果
問題 | No.2180 Comprehensive Line Segments |
ユーザー |
![]() |
提出日時 | 2022-10-12 18:04:17 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,808 bytes |
コンパイル時間 | 428 ms |
コンパイル使用メモリ | 82,536 KB |
実行使用メモリ | 1,000,264 KB |
最終ジャッジ日時 | 2024-11-17 01:07:06 |
合計ジャッジ時間 | 64,782 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 MLE * 2 |
other | AC * 8 TLE * 6 MLE * 11 |
ソースコード
import sysfrom collections import dequefrom fractions import Fraction as fracinput = sys.stdin.readlineINF = 10 ** 9class Vector2:def __init__(self, x: frac, y: frac):self.x = xself.y = ydef __eq__(self, other):return (self.x == other.x and self.y == other.y)def __hash__(self):return hash((self.x, self.y))def __sub__(self, other):return Vector2(other.x - self.x, other.y - self.y)class Line:def __init__(self, a: frac, b:frac, c:frac):self.a = aself.b = bself.c = cdef __eq__(self, other):return (self.a == other.a and self.b == other.b and self.c == other.c)def __hash__(self):return hash((self.a, self.b, self.c))def be_same_inclination(one: Vector2, other: Vector2) -> bool:if(one.x == 0):return (other.x == 0 and one.y / other.y > 0)a = other.x / one.xreturn (a * one.y == other.y and a > 0)def calc_line(one: Vector2, other: Vector2) -> Line:x1 = one.x; y1 = one.yx2 = other.x; y2 = other.yif(x1 == x2):return Line(frac(1), frac(0), x1)a = (y1 - y2) / (x1 - x2)c = y1 - a * x1return Line(-a, frac(1), c)def calc_intersection(one: Line, other: Line) -> Vector2:p = one.a * other.b - other.a * one.bif(p == frac(0)):return Noneq = other.b * one.c - one.b * other.cx = q / py = (other.c - other.a * x) / other.b if(one.b == 0) else (one.c - one.a * x) / one.breturn Vector2(x, y)"""Main Code"""# 入力N = int(input())P = [Vector2(*map(frac, input().split())) for _ in [0] * N]# 点が1個のときは必ず答え1if(N == 1):print(1)exit(0)# 各点に番号付けph = {}pt_id = 0for p in P:ph[p] = pt_idpt_id += 1# 有り得る直線を調べて番号付けln_id = 0lh = {}for i in range(N - 1):for j in range(i + 1, N):p1, p2 = P[i], P[j]l = calc_line(p1, p2)if(l in lh):continuelh[l] = ln_idln_id += 1# 有り得る交点を調べて番号付けlis = list(lh.keys())for i in range(ln_id - 1):for j in range(i + 1, ln_id):l1, l2 = lis[i], lis[j]p = calc_intersection(l1, l2)if(p is None or p in ph):continueP.append(p)ph[p] = pt_idpt_id += 1# グラフ探索dp = [[[INF]*pt_id for _ in [0]*pt_id] for _ in [0]*(1 << N)]que = deque([])for i in range(N):que.append((0, 1 << i, i, i))dp[1 << i][i][i] = 0goal = (1 << N) - 1ans = INFwhile(que):c, b, lv, v = que.popleft()if(c > dp[b][lv][v]):continueif(b == goal):ans = cbreakfor nv in range(pt_id):if((nv in [lv, v]) or min(v, nv) >= N):continuenb = b | (1 << nv) if(nv < N) else bnc = cif(lv == v or not(be_same_inclination(P[v] - P[lv], P[nv] - P[v]))):nc += 1if(nc >= dp[nb][v][nv]):continuedp[nb][v][nv] = ncif(nc == c):que.appendleft((nc, nb, v, nv))else:que.append((nc, nb, v, nv))print(ans)