結果

問題 No.54 Happy Hallowe'en
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-02 18:20:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 108 ms / 5,000 ms
コード長 1,565 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 84,608 KB
最終ジャッジ日時 2024-04-23 16:42:21
合計ジャッジ時間 2,002 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,352 KB
testcase_01 AC 38 ms
52,400 KB
testcase_02 AC 38 ms
52,400 KB
testcase_03 AC 37 ms
52,480 KB
testcase_04 AC 74 ms
71,424 KB
testcase_05 AC 85 ms
75,136 KB
testcase_06 AC 84 ms
74,880 KB
testcase_07 AC 84 ms
75,008 KB
testcase_08 AC 97 ms
77,696 KB
testcase_09 AC 102 ms
77,568 KB
testcase_10 AC 35 ms
52,352 KB
testcase_11 AC 36 ms
52,224 KB
testcase_12 AC 108 ms
84,608 KB
testcase_13 AC 80 ms
77,552 KB
testcase_14 AC 37 ms
52,352 KB
testcase_15 AC 36 ms
52,096 KB
testcase_16 AC 49 ms
61,952 KB
testcase_17 AC 61 ms
67,200 KB
testcase_18 AC 36 ms
52,096 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