結果
| 問題 |
No.54 Happy Hallowe'en
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-08-02 18:22:02 |
| 言語 | Python2 (2.7.18) |
| 結果 |
AC
|
| 実行時間 | 83 ms / 5,000 ms |
| コード長 | 1,574 bytes |
| コンパイル時間 | 636 ms |
| コンパイル使用メモリ | 6,784 KB |
| 実行使用メモリ | 16,384 KB |
| 最終ジャッジ日時 | 2024-10-15 17:13:30 |
| 合計ジャッジ時間 | 1,843 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
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))