結果

問題 No.54 Happy Hallowe'en
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-02 18:20:20
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 127 ms / 5,000 ms
コード長 1,565 bytes
コンパイル時間 309 ms
コンパイル使用メモリ 86,812 KB
実行使用メモリ 86,784 KB
最終ジャッジ日時 2023-08-05 20:08:20
合計ジャッジ時間 3,067 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
70,688 KB
testcase_01 AC 66 ms
70,920 KB
testcase_02 AC 67 ms
71,008 KB
testcase_03 AC 70 ms
70,920 KB
testcase_04 AC 113 ms
76,712 KB
testcase_05 AC 112 ms
77,884 KB
testcase_06 AC 113 ms
78,016 KB
testcase_07 AC 111 ms
78,380 KB
testcase_08 AC 113 ms
78,240 KB
testcase_09 AC 121 ms
78,364 KB
testcase_10 AC 65 ms
70,908 KB
testcase_11 AC 69 ms
70,908 KB
testcase_12 AC 127 ms
86,784 KB
testcase_13 AC 96 ms
77,980 KB
testcase_14 AC 64 ms
71,192 KB
testcase_15 AC 65 ms
70,860 KB
testcase_16 AC 79 ms
75,512 KB
testcase_17 AC 85 ms
75,740 KB
testcase_18 AC 67 ms
70,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(1000000)

def read_data():
    N = int(input())
    VT = []
    for n in range(N):
        v, t = map(int, input().split())
        VT.append((v + t, v, t))
    return N, VT

def solve(N, VT):
    VT.sort(reverse=True)
    candidate = min(VT[0][0] - 1, sum(v for vt, v, t in VT))
    while not is_valid(candidate, VT):
        candidate -= 1
    return candidate

def is_valid(val, VT):
    head = 0
    nexts = list(range(1, len(VT) + 1))
    prevs = list(range(-1, len(VT) - 1))
    lower = val
    upper = float('inf')
    return dfs(lower, upper, VT, head, nexts, prevs)

def dfs(lower, upper, VT, head, nexts, prevs):
    '''VTのうち、used でないものを使って、lower 以上、upper 未満の選び方をできるかを返す。
    '''
    if lower == 0:
        return True
    if lower < 0:
        return False
    i = head
    end = len(VT)
    while i != end:
        val, v, t = VT[i]
        if v >= upper:
            i = nexts[i]
            continue
        if val < lower:
            return False
        if i == head:
            nhead = nexts[i]
        else:
            nhead = head
            nexts[prevs[i]] = nexts[i]
        if nexts[i] != end:
            prevs[nexts[i]] = prevs[i]
        result = dfs(lower - v, min(upper, t), VT, nhead, nexts, prevs)
        if i != head:
            nexts[prevs[i]] = i
        if nexts[i] != end:
            prevs[nexts[i]] = i
        if result:
            return True
        i = nexts[i]
    return False


N, VT = read_data()

print(solve(N, VT))
0