結果
問題 | No.2026 Yet Another Knapsack Problem |
ユーザー | chineristAC |
提出日時 | 2022-04-24 13:48:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 6,174 ms / 10,000 ms |
コード長 | 2,561 bytes |
コンパイル時間 | 200 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 143,736 KB |
最終ジャッジ日時 | 2024-07-21 22:37:26 |
合計ジャッジ時間 | 52,512 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 141 ms
88,704 KB |
testcase_01 | AC | 136 ms
88,960 KB |
testcase_02 | AC | 142 ms
88,960 KB |
testcase_03 | AC | 200 ms
90,496 KB |
testcase_04 | AC | 182 ms
90,240 KB |
testcase_05 | AC | 140 ms
88,960 KB |
testcase_06 | AC | 185 ms
90,240 KB |
testcase_07 | AC | 189 ms
90,368 KB |
testcase_08 | AC | 155 ms
89,600 KB |
testcase_09 | AC | 189 ms
90,112 KB |
testcase_10 | AC | 175 ms
90,240 KB |
testcase_11 | AC | 193 ms
90,624 KB |
testcase_12 | AC | 131 ms
88,700 KB |
testcase_13 | AC | 183 ms
90,112 KB |
testcase_14 | AC | 176 ms
90,368 KB |
testcase_15 | AC | 129 ms
88,832 KB |
testcase_16 | AC | 180 ms
90,496 KB |
testcase_17 | AC | 177 ms
90,404 KB |
testcase_18 | AC | 133 ms
88,892 KB |
testcase_19 | AC | 172 ms
90,368 KB |
testcase_20 | AC | 132 ms
88,960 KB |
testcase_21 | AC | 174 ms
90,476 KB |
testcase_22 | AC | 165 ms
89,856 KB |
testcase_23 | AC | 191 ms
90,240 KB |
testcase_24 | AC | 132 ms
88,940 KB |
testcase_25 | AC | 135 ms
88,704 KB |
testcase_26 | AC | 176 ms
90,240 KB |
testcase_27 | AC | 165 ms
89,856 KB |
testcase_28 | AC | 354 ms
91,648 KB |
testcase_29 | AC | 368 ms
92,032 KB |
testcase_30 | AC | 349 ms
91,904 KB |
testcase_31 | AC | 346 ms
91,904 KB |
testcase_32 | AC | 314 ms
91,648 KB |
testcase_33 | AC | 357 ms
92,260 KB |
testcase_34 | AC | 331 ms
92,020 KB |
testcase_35 | AC | 313 ms
91,520 KB |
testcase_36 | AC | 339 ms
91,776 KB |
testcase_37 | AC | 347 ms
91,648 KB |
testcase_38 | AC | 5,636 ms
140,160 KB |
testcase_39 | AC | 6,174 ms
143,736 KB |
testcase_40 | AC | 6,154 ms
141,312 KB |
testcase_41 | AC | 6,022 ms
142,812 KB |
testcase_42 | AC | 6,089 ms
141,056 KB |
testcase_43 | AC | 6,133 ms
141,276 KB |
testcase_44 | AC | 6,127 ms
143,104 KB |
ソースコード
import sys,random,bisect from collections import deque,defaultdict from heapq import heapify,heappop,heappush from itertools import permutations from math import gcd from fractions import Fraction input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) local_test = False def random_test(N): item = [(N,random.randint(-10**2,10**2))] for i in range(1,N): c = random.randint(1,2) v = random.randint(-10**2,10**2) item.append((c,v)) return item def solve_slow(N,item): dp = [[-INF]*(N+1) for w in range(N+1)] dp[0][0] = 0 for i in range(N): c,v = item[i] for w in range(N+1)[::-1]: for k in range(N+1)[::-1]: for use in range(c+1): if w+use*(i+1) <= N and k+use <= N: dp[w+use*(i+1)][k+use] = max(dp[w+use*(i+1)][k+use],dp[w][k]+use*v) return [max(dp[w][k] for w in range(N+1)) for k in range(1,N+1)] def solve(N,item): dp = [[-INF]*(N+1) for i in range(N+1)] dp[0][0] = 0 for i in range(N)[::-1]: c,v = item[i] k_max = N//(i+1) for w_start in range(i+1): for k_start in range(k_max+1): deq = deque() for k in range(k_start,k_max+1): w = w_start + (k-k_start) * (i+1) if N < w: break if deq and deq[0][1] < k-c: deq.popleft() tmp = dp[w][k] - k * v while deq and deq[-1][0] <= tmp: deq.pop() deq.append((tmp,k)) dp[w][k] = v*k + deq[0][0] for w_start in range(i+1,N+1): for k_start in range(1): deq = deque() for k in range(k_start,k_max+1): w = w_start + (k-k_start) * (i+1) if N < w: break if deq and deq[0][1] < k-c: deq.popleft() tmp = dp[w][k] - k * v while deq and deq[-1][0] <= tmp: deq.pop() deq.append((tmp,k)) dp[w][k] = v*k + deq[0][0] res = [max(dp[w][k] for w in range(N+1)) for k in range(1,N+1)] return res INF = 10**17 N = int(input()) item = [tuple(mi()) for i in range(N)] print(*solve(N,item),sep="\n")