結果

問題 No.626 Randomized 01 Knapsack
ユーザー ああいいああいい
提出日時 2022-04-28 10:37:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,368 bytes
コンパイル時間 1,256 ms
コンパイル使用メモリ 86,760 KB
実行使用メモリ 81,176 KB
最終ジャッジ日時 2023-09-10 16:47:43
合計ジャッジ時間 4,940 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,276 KB
testcase_01 AC 70 ms
71,292 KB
testcase_02 AC 72 ms
71,240 KB
testcase_03 AC 73 ms
71,192 KB
testcase_04 AC 78 ms
71,344 KB
testcase_05 AC 75 ms
71,420 KB
testcase_06 AC 82 ms
71,708 KB
testcase_07 AC 123 ms
76,760 KB
testcase_08 AC 91 ms
76,088 KB
testcase_09 AC 119 ms
78,680 KB
testcase_10 AC 125 ms
79,916 KB
testcase_11 AC 126 ms
80,328 KB
testcase_12 AC 132 ms
80,852 KB
testcase_13 AC 97 ms
76,956 KB
testcase_14 AC 107 ms
78,744 KB
testcase_15 AC 125 ms
80,284 KB
testcase_16 AC 130 ms
80,456 KB
testcase_17 AC 130 ms
80,792 KB
testcase_18 AC 128 ms
80,836 KB
testcase_19 AC 132 ms
80,844 KB
testcase_20 AC 132 ms
81,176 KB
testcase_21 AC 97 ms
76,884 KB
testcase_22 AC 134 ms
80,312 KB
testcase_23 AC 137 ms
81,088 KB
testcase_24 AC 95 ms
76,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,W = map(int,input().split())
l = []
for _ in range(n):
    v,w = map(int,input().split())
    if w <= W:
        l.append((v,w))
n = len(l)
l.sort(key = lambda x:x[0] / x[1],reverse = True)

def kanwa(i,nowv,noww):
    flag = False
    for j in range(i,n):
        v,w = l[j]
        if w == W - noww:
            flag = True
        if w < W - noww:
            nowv += v
            noww += w
        else:
            rate = (W - noww) / w
            nowv += v * rate
            noww = W
            break
    return nowv,flag

import sys
sys.setrecursionlimit(10 ** 8)
zanteikai = 0
inf = 10 ** 18
def calc(i,nowv,noww):
    global zanteikai
    if i == n:
        if nowv > zanteikai:
            zanteikai = nowv
        return nowv
    kanwakai,flag = kanwa(i,nowv,noww)
    if flag:
        if kanwakai > zanteikai:
            zanteikai = kanwakai
        return int(kanwakai)
    if kanwakai < zanteikai:
        return -inf
    v,w = l[i]
    tmp = -inf
    if noww + w <= W:
        u = calc(i + 1,nowv + v,noww + w)
        if u > tmp:
            tmp = u
        if tmp > zanteikai:
            zanteikai = tmp
    u = calc(i + 1,nowv,noww)
    if u > tmp:
        tmp = u
    if tmp > zanteikai:
        zanteikai = tmp
    return tmp


now = 0
for v,w in l:
    if now + w <= W:
        now += w
        zanteikai += v
ans = calc(0,0,0)
print(ans)
0