結果

問題 No.497 入れ子の箱
ユーザー brthyyjp
提出日時 2021-02-11 12:49:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 286 ms / 5,000 ms
コード長 1,370 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 79,232 KB
最終ジャッジ日時 2024-07-18 01:57:57
合計ジャッジ時間 7,122 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

def main():

    n = int(input())
    XYZ = []
    X = []
    for i in range(n):
        temp = list(map(int, input().split()))
        temp.sort()
        x, y, z = temp
        XYZ.append((x, y, z))
        X.append(x)

    X = sorted(list(set(X)))
    d = {}
    for i, x in enumerate(X):
        d[x] = i

    N = len(d)
    YZ = [[] for i in range(N)]
    for x, y, z in XYZ:
        YZ[d[x]].append((y, z))

    from collections import defaultdict
    dp = defaultdict(lambda:1)
    mod =10**9
    for y, z in YZ[0]:
        dp[y*mod+z] = 1
        dp[z*mod+y] = 1
    for i in range(1, N):
        nx = defaultdict(lambda:1)
        for k, v in dp.items():
            nx[k] = max(nx[k], v)
        for ny, nz in YZ[i]:
            nk = ny*mod+nz
            nx[nk] = 1
            nk = nz*mod+ny
            nx[nk] = 1
            for k, v in dp.items():
                y, z = divmod(k, mod)
                if y < ny and z < nz:
                    nk = ny*mod+nz
                    nx[nk] = max(nx[nk], dp[k]+1)
                if y < nz and z < ny:
                    nk = nz*mod+ny
                    nx[nk] = max(nx[nk], dp[k]+1)

        dp = nx
    ans = 1
    for k, v in dp.items():
        ans = max(ans, v)
    print(ans)

if __name__ == '__main__':
    main()
0