結果

問題 No.54 Happy Hallowe'en
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-02 18:18:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 97 ms / 5,000 ms
コード長 1,565 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2024-04-23 16:42:13
合計ジャッジ時間 1,543 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
10,880 KB
testcase_01 AC 28 ms
10,752 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 29 ms
10,880 KB
testcase_04 AC 38 ms
11,392 KB
testcase_05 AC 43 ms
11,776 KB
testcase_06 AC 51 ms
12,160 KB
testcase_07 AC 52 ms
12,416 KB
testcase_08 AC 77 ms
12,672 KB
testcase_09 AC 97 ms
13,184 KB
testcase_10 AC 28 ms
10,880 KB
testcase_11 AC 27 ms
10,880 KB
testcase_12 AC 69 ms
15,488 KB
testcase_13 AC 63 ms
13,056 KB
testcase_14 AC 28 ms
10,880 KB
testcase_15 AC 29 ms
10,880 KB
testcase_16 AC 29 ms
10,752 KB
testcase_17 AC 36 ms
10,880 KB
testcase_18 AC 28 ms
10,752 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