結果

問題 No.2180 Comprehensive Line Segments
ユーザー MasKoaTSMasKoaTS
提出日時 2023-08-13 12:09:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,759 ms / 5,000 ms
コード長 3,982 bytes
コンパイル時間 242 ms
コンパイル使用メモリ 82,556 KB
実行使用メモリ 90,536 KB
最終ジャッジ日時 2024-05-01 05:27:00
合計ジャッジ時間 18,817 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
76,832 KB
testcase_01 AC 37 ms
55,060 KB
testcase_02 AC 34 ms
53,724 KB
testcase_03 AC 1,622 ms
87,920 KB
testcase_04 AC 36 ms
54,320 KB
testcase_05 AC 34 ms
53,800 KB
testcase_06 AC 34 ms
54,348 KB
testcase_07 AC 35 ms
54,716 KB
testcase_08 AC 35 ms
55,524 KB
testcase_09 AC 963 ms
86,964 KB
testcase_10 AC 1,041 ms
88,784 KB
testcase_11 AC 1,710 ms
89,792 KB
testcase_12 AC 592 ms
82,120 KB
testcase_13 AC 1,673 ms
88,412 KB
testcase_14 AC 1,463 ms
90,536 KB
testcase_15 AC 1,759 ms
89,792 KB
testcase_16 AC 278 ms
80,164 KB
testcase_17 AC 306 ms
79,988 KB
testcase_18 AC 1,612 ms
88,384 KB
testcase_19 AC 579 ms
82,684 KB
testcase_20 AC 45 ms
64,248 KB
testcase_21 AC 332 ms
81,120 KB
testcase_22 AC 85 ms
76,932 KB
testcase_23 AC 181 ms
78,556 KB
testcase_24 AC 135 ms
77,656 KB
testcase_25 AC 216 ms
79,384 KB
testcase_26 AC 1,590 ms
89,780 KB
testcase_27 AC 467 ms
81,944 KB
testcase_28 AC 466 ms
82,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from __future__ import annotations
import sys
from itertools import permutations
input = sys.stdin.readline


class Vector2:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return str((self.x, 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 __neg__(self):
        return Vector2(-self.x, -self.y)

    def __add__(self, other):
        return Vector2(self.x + other.x, self.y + other.y)

    def __sub__(self, other):
        return Vector2(self.x - other.x, self.y - other.y)

    def __mul__(self, other):
        return self.x * other.y - self.y * other.x

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


def sgn(x: int) -> int:
    if(x > 0):
        return 1
    if(x < 0):
        return -1
    return 0

def same_inclination(v1: Vector2, v2: Vector2) -> bool:
    return (v1 * v2 == 0 and (v1.x * v2.x > 0 or v1.y * v2.y > 0))


"""
Main Code
"""

N = int(input())
P = [Vector2(*map(int, input().split())) for _ in [0] * N]

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

perm = [*permutations([*range(N)], 2)]
vectors = [[None]*N for _ in [0]*N]
for i, j in perm:
    vectors[i][j] = P[j] - P[i]

ans = N
dp = [[[N]*N for _ in [0]*N] for _ in [0]*(1 << N)]
for i, j in perm:
    dp[(1 << i) | (1 << j)][i][j] = 1

goal = (1 << N) - 1
for b_now in range(3, goal):
    for v_prev, v_now in perm:
        if(dp[b_now][v_prev][v_now] == N or not((b_now >> v_prev) & 1) or not((b_now >> v_now) & 1)):
            continue
        c_next = c_now = dp[b_now][v_prev][v_now]
        v1 = vectors[v_prev][v_now]
        for v_next1, v_next2 in perm:
            if((((b_now >> v_next1) & 1) and v_now != v_next1) or ((b_now >> v_next2) & 1)):
                continue
            b_next = b_now | (1 << v_next1) | (1 << v_next2)
            v2, v3 = vectors[v_now][v_next1], vectors[v_next1][v_next2]
            d = e = 1
            if(v_now == v_next1):
                c_next = c_now + 1 - same_inclination(v1, v3)
            else:
                d = e = same_inclination(v1, v2) + same_inclination(v2, v3)
                if(e == 0):
                    e = int(sgn(v1 * v2) == sgn(v2 * v3) == sgn(v1 * v3))
                c_next = c_now + 2 - e
            # print(b_now, b_next, v_prev, v_now, v_next1, v_next2, c_now, c_next)
            if(dp[b_next][v_next1][v_next2] > c_next):
                dp[b_next][v_next1][v_next2] = c_next
            # one point
            if(v_now == v_next1 or d != 0):
                continue
            c_in = c_next + e
            for v_in in range(N):
                if((b_next >> v_in) & 1):
                    continue
                v4 = vectors[v_now][v_in]
                v5 = vectors[v_next1][v_in]
                # if(v_in == 11 and v_prev == 9 and v_now == 10 and sgn(v1 * v4) == sgn(v4 * v2)):
                #    print(P[v_prev], P[v_now], P[v_in], P[v_next1], P[v_next2])
                if not(sgn(v1 * v4) == sgn(v4 * v2) == sgn(v1 * v2)):
                    continue
                #if(v_in == 11 and v_prev == 9 and v_now == 10 and sgn((-v2) * v5) == sgn(v5 * (-v3))):
                #    print(P[v_prev], P[v_now], P[v_in], P[v_next1], P[v_next2])
                if not(sgn((-v2) * v5) == sgn(v5 * (-v3)) == sgn((-v2) * (-v3))):
                    continue
                b_in = b_next | (1 << v_in)
                if(dp[b_in][v_next1][v_next2] <= c_in):
                    continue
                #if(v_in == 11 and b_in == 0b111000111111):
                #    print(c_in, bin(b_in), P[v_prev], P[v_now], P[v_in], P[v_next1], P[v_next2])
                dp[b_in][v_next1][v_next2] = c_in

ans = min(min(lis) for lis in dp[goal])
print(ans)
0