結果

問題 No.54 Happy Hallowe'en
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-02 18:02:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,719 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 13,340 KB
最終ジャッジ日時 2023-09-25 00:34:21
合計ジャッジ時間 1,648 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 17 ms
8,032 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 16 ms
8,072 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

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)
    upper = min(VT[0][0], sum(v for vt, v, t in VT) + 1)
    lower = 1
    mid = upper - 1
    while lower + 1 < upper:
        if is_valid(mid, VT):
            lower = mid
        else:
            upper = mid
        mid = (upper + lower) // 2
    return lower

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 未満の選び方をできるかを返す。
    '''
    print(head)
    if lower <= 0:
        return True
    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()
#N = 4040
#VT = [(1 + i, 1, i) for i in range(1, N + 1)]

print(solve(N, VT))
0