結果

問題 No.2180 Comprehensive Line Segments
ユーザー MasKoaTSMasKoaTS
提出日時 2022-11-30 18:22:42
言語 PyPy3
(7.3.15)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,831 bytes
コンパイル時間 572 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 1,201,476 KB
最終ジャッジ日時 2024-11-17 01:30:21
合計ジャッジ時間 68,153 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 296 ms
102,944 KB
testcase_01 MLE -
testcase_02 AC 140 ms
95,732 KB
testcase_03 MLE -
testcase_04 AC 175 ms
89,176 KB
testcase_05 AC 132 ms
88,972 KB
testcase_06 AC 136 ms
89,096 KB
testcase_07 AC 135 ms
88,916 KB
testcase_08 AC 131 ms
89,140 KB
testcase_09 AC 213 ms
100,128 KB
testcase_10 AC 327 ms
104,256 KB
testcase_11 MLE -
testcase_12 MLE -
testcase_13 TLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 TLE -
testcase_17 MLE -
testcase_18 TLE -
testcase_19 MLE -
testcase_20 AC 268 ms
91,804 KB
testcase_21 MLE -
testcase_22 AC 411 ms
94,824 KB
testcase_23 AC 2,335 ms
221,056 KB
testcase_24 AC 888 ms
115,544 KB
testcase_25 TLE -
testcase_26 MLE -
testcase_27 WA -
testcase_28 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from __future__ import annotations
import itertools as iter
import collections as coll
import heapq as hq
import bisect as bis
from decimal import Decimal as dec
from functools import cmp_to_key
import math
import sys
#import pypyjit
#pypyjit.set_param('max_unroll_recursion=-1')
sys.setrecursionlimit(10 ** 6)
inp = sys.stdin.readline
input = lambda : inp().rstrip()
getN = lambda : int(inp())
getNs = lambda : map(int, inp().split())
getList = lambda :list(map(int, inp().split()))
getStrs = lambda n : [input() for _ in [0] * n]
def yexit(): print("Yes"); exit(0)
def nexit(): print("No"); exit(0)
pi = 3.141592653589793
mod = 1000000007
MOD = 998244353
INF = 4611686018427387903
dx = [1, 0, -1, 0];	dy = [0, 1, 0, -1]
#di = coll.defaultdict(int)

from fractions import Fraction as frac
class Vector2:
	def __init__(self, x, y):
		self.x = x
		self.y = y

	def __str__(self):
		return str((float(self.x), float(self.y)))

	__repr__ = __str__

	def __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)

	def normalize(self) -> Vector2:
		if(self.x == 0 and self.y == 0):
			return self
		norm = self.x ** 2 + self.y ** 2
		self.x *= abs(self.x) / norm
		self.y *= abs(self.y) / norm
		return self


class Line:
	def __init__(self, a, b, c):
		self.a = a
		self.b = b
		self.c = c

	def __str__(self):
		return str((self.a, self.b, self.c))

	__repr__ = __str__

	def __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))
	
	@classmethod
	def calc_line(cls, one: Vector2, other: Vector2) -> Line:
		if(one == other):
			return None
		x1 = one.x;  y1 = one.y
		x2 = other.x;  y2 = other.y
		if(x1 == x2):
			return Line(1, 0, x1)
		a = (y1 - y2) / (x1 - x2)
		c = y1 - a * x1
		return Line(-a, 1, c)

	@classmethod
	def calc_intersection(cls, one: Line, other: Line) -> Vector2:
		p = one.a * other.b - other.a * one.b
		if(p == 0):
			return None
		q = other.b * one.c - one.b * other.c
		x = q / p
		y = (other.c - other.a * x) / other.b if(one.b == 0) else (one.c - one.a * x) / one.b
		return Vector2(x, y)

"""
Main Code
"""

N = getN()
P = [Vector2(*map(frac, inp().split())) for _ in [0]*N]

if(N == 1):
	print(1)
	exit(0)

line_set = set([])
for p1, p2 in iter.combinations(P, 2):
	line_set.add(Line.calc_line(p1, p2))
	
point_set = set(P)
point_num = N
for l1, l2 in iter.combinations(line_set, 2):
	p = Line.calc_intersection(l1, l2)
	if(p is None or p in point_set):
		continue
	point_set.add(p)
	P.append(p)
	point_num += 1

vec_lis = [[None]*point_num for _ in [0]*point_num]
for i in range(point_num - 1):
	for j in range(i + 1, point_num):
		if(Line.calc_line(P[i], P[j]) not in line_set):
			continue
		vec_lis[i][j] = (P[j] - P[i]).normalize()
		vec_lis[j][i] = (P[i] - P[j]).normalize()

mbit = 1 << N
goal = mbit - 1
dp = [[[N]*point_num for _ in [0]*point_num] for _ in [0]*mbit]
que = coll.deque([])
for i in range(N):
	bit = 1 << i
	dp[bit][i][i] = 0
	que.append((bit, i, i, 0))
while(que):
	b_now, v_prev, v_now, c_now = que.popleft()
	if(c_now > dp[b_now][v_prev][v_now]):
		continue
	if(b_now == goal):
		print(c_now)
		break
	for v_next in range(point_num):
		if(v_next in [v_prev, v_now] or min(v_now, v_next) >= N):
			continue
		if(vec_lis[v_now][v_next] is None):
			continue
		b_next = b_now | (1 << v_next) if(v_next < N) else b_now
		c_next = c_now + (v_prev == v_now or vec_lis[v_prev][v_now] != vec_lis[v_now][v_next])
		if(c_next >= dp[b_next][v_now][v_next]):
			continue
		dp[b_next][v_now][v_next] = c_next
		if(c_now == c_next):
			que.appendleft((b_next, v_now, v_next, c_next))
		else:
			que.append((b_next, v_now, v_next, c_next))
0