結果

問題 No.1045 直方体大学
ユーザー lam6er
提出日時 2025-04-15 23:38:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 887 ms / 2,000 ms
コード長 1,955 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,136 KB
実行使用メモリ 194,644 KB
最終ジャッジ日時 2025-04-15 23:39:27
合計ジャッジ時間 5,198 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n = int(input())
prisms = []
for _ in range(n):
    a, b, c = map(int, input().split())
    bases = []
    # Generate all three possible base pairs (sorted) and their heights
    x, y = max(a, b), min(a, b)
    bases.append((x, y, c))
    x, y = max(a, c), min(a, c)
    bases.append((x, y, b))
    x, y = max(b, c), min(b, c)
    bases.append((x, y, a))
    prisms.append(bases)

# DP array: each mask maps to a dictionary of (max_dim, min_dim) -> max_height
dp = [{} for _ in range(1 << n)]
queue = deque()
max_height = 0

# Initialize with each possible starting state (single prism)
for i in range(n):
    for base in prisms[i]:
        max_p, min_p, h = base
        mask = 1 << i
        key = (max_p, min_p)
        if key not in dp[mask] or dp[mask][key] < h:
            dp[mask][key] = h
            queue.append((mask, max_p, min_p, h))
            if h > max_height:
                max_height = h

# Process each state in the queue
while queue:
    mask, curr_max, curr_min, height = queue.popleft()
    # Update the global maximum height
    if height > max_height:
        max_height = height
    # Try adding each possible next prism
    for j in range(n):
        if not (mask & (1 << j)):
            # Prism j is not used yet
            for base in prisms[j]:
                new_max, new_min, new_h = base
                if new_max <= curr_max and new_min <= curr_min:
                    new_mask = mask | (1 << j)
                    total_h = height + new_h
                    key = (new_max, new_min)
                    # Check if this state is better than existing
                    if key not in dp[new_mask] or dp[new_mask].get(key, 0) < total_h:
                        dp[new_mask][key] = total_h
                        queue.append((new_mask, new_max, new_min, total_h))
                        if total_h > max_height:
                            max_height = total_h

print(max_height)
0