結果

問題 No.54 Happy Hallowe'en
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-02 18:22:02
言語 Python2
(2.7.18)
結果
AC  
実行時間 89 ms / 5,000 ms
コード長 1,574 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 16,768 KB
最終ジャッジ日時 2024-04-23 16:42:28
合計ジャッジ時間 1,430 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
6,144 KB
testcase_01 AC 9 ms
6,272 KB
testcase_02 AC 10 ms
6,144 KB
testcase_03 AC 9 ms
6,144 KB
testcase_04 AC 22 ms
6,784 KB
testcase_05 AC 26 ms
7,040 KB
testcase_06 AC 33 ms
7,424 KB
testcase_07 AC 33 ms
7,680 KB
testcase_08 AC 61 ms
7,936 KB
testcase_09 AC 89 ms
8,576 KB
testcase_10 AC 10 ms
6,144 KB
testcase_11 AC 10 ms
6,144 KB
testcase_12 AC 52 ms
16,768 KB
testcase_13 AC 36 ms
8,448 KB
testcase_14 AC 10 ms
6,144 KB
testcase_15 AC 9 ms
6,144 KB
testcase_16 AC 11 ms
6,144 KB
testcase_17 AC 20 ms
6,144 KB
testcase_18 AC 9 ms
6,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(1000000)

def read_data():
    N = int(raw_input())
    VT = []
    for n in xrange(N):
        v, t = map(int, raw_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