結果

問題 No.626 Randomized 01 Knapsack
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-30 01:13:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 807 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 13,688 KB
最終ジャッジ日時 2024-05-02 01:43:07
合計ジャッジ時間 2,647 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
10,880 KB
testcase_01 AC 26 ms
10,624 KB
testcase_02 AC 27 ms
10,752 KB
testcase_03 AC 28 ms
10,880 KB
testcase_04 AC 26 ms
10,880 KB
testcase_05 AC 29 ms
10,880 KB
testcase_06 AC 33 ms
10,880 KB
testcase_07 AC 37 ms
11,264 KB
testcase_08 AC 35 ms
11,136 KB
testcase_09 AC 44 ms
11,904 KB
testcase_10 AC 37 ms
11,904 KB
testcase_11 AC 54 ms
13,568 KB
testcase_12 AC 72 ms
13,684 KB
testcase_13 AC 66 ms
13,688 KB
testcase_14 AC 52 ms
13,440 KB
testcase_15 AC 66 ms
13,556 KB
testcase_16 AC 69 ms
13,684 KB
testcase_17 AC 66 ms
13,688 KB
testcase_18 AC 66 ms
13,688 KB
testcase_19 AC 71 ms
13,684 KB
testcase_20 AC 84 ms
13,548 KB
testcase_21 AC 72 ms
13,560 KB
testcase_22 AC 87 ms
13,548 KB
testcase_23 AC 95 ms
13,688 KB
testcase_24 AC 55 ms
13,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 7)

n, cap = map(int, input().split())
best = 0
goods = [list(map(int, input().split())) for i in range(n)]
goods.sort(key=lambda x: x[0] / x[1], reverse=True)
v_cum = [0]
def dfs(ind, v, w):
    global best
    if w > cap: return
    if ind == n: best = max(best, v); return
    v_l = v
    w_new = cap - w
    cur = ind
    while cur < n and 0 <= w_new - goods[cur][1]:
        w_new -= goods[cur][1]
        v_l += goods[cur][0]
        cur += 1
    cur -= 1
    if cur == n and w_new == 0: bese = max(best, v_l); return 
    opt = v_l + goods[cur][0] * (w_new / goods[cur][1])
    if opt <= best: return 
    dfs(ind + 1, v + goods[ind][0], w + goods[ind][1])
    dfs(ind + 1, v, w)
for i in range(n): v_cum.append(v_cum[-1] + goods[i][0])
dfs(0, 0, 0)
print(best)
0