結果
| 問題 | No.3401 Large Knapsack Problem |
| コンテスト | |
| ユーザー |
detteiuu
|
| 提出日時 | 2025-12-10 01:52:00 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 539 ms / 2,000 ms |
| コード長 | 2,080 bytes |
| 記録 | |
| コンパイル時間 | 275 ms |
| コンパイル使用メモリ | 82,672 KB |
| 実行使用メモリ | 107,672 KB |
| 最終ジャッジ日時 | 2025-12-10 01:52:22 |
| 合計ジャッジ時間 | 20,720 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
from math import gcd
from functools import cmp_to_key
class Fraction:
def __init__(self, top, bottom):
self.top = top
self.bottom = bottom
def to_fraction(n):
if type(n) == int:
return Fraction(n, 1)
else:
return n
def compare(L, R):
L, R = to_fraction(L), to_fraction(R)
a = L.top*R.bottom
b = L.bottom*R.top
if a < b:
return -1
elif a == b:
return 0
else:
return 1
def max_f(L, R):
return L if compare(L, R) == 1 else R
def min_f(L, R):
return L if compare(L, R) == -1 else R
def ADD(L, R):
L, R = to_fraction(L), to_fraction(R)
l, r = L.bottom, R.bottom
lt = L.top*r
rt = R.top*l
top = lt+rt
bottom = l*r
GCD = gcd(top, bottom)
top //= GCD
bottom //= GCD
return Fraction(top, bottom)
def SUB(L, R):
L, R = to_fraction(L), to_fraction(R)
l, r = L.bottom, R.bottom
lt = L.top*r
rt = R.top*l
top = lt-rt
bottom = l*r
GCD = gcd(top, bottom)
top //= GCD
bottom //= GCD
return Fraction(top, bottom)
def MUL(L, R):
L, R = to_fraction(L), to_fraction(R)
top = L.top*R.top
bottom = L.bottom*R.bottom
GCD = gcd(top, bottom)
top //= GCD
bottom //= GCD
return Fraction(top, bottom)
def DIV(L, R):
L, R = to_fraction(L), to_fraction(R)
top = L.top*R.bottom
bottom = L.bottom*R.top
GCD = gcd(top, bottom)
top //= GCD
bottom //= GCD
return Fraction(top, bottom)
INF = 1<<60
N, W = map(int, input().split())
item = [list(map(int, input().split())) for _ in range(N)]
L = 10**6*2
dp = [-INF]*(L+1)
dp[0] = 0
for v, w in item:
for i in range(L+1):
if dp[i] == -INF:
continue
if i+w <= L:
dp[i+w] = max(dp[i+w], dp[i]+v)
ans = 0
if L < W:
cm = cmp_to_key(compare)
for i in range(N):
item[i] = Fraction(*item[i])
item.sort(key=cm, reverse=True)
v, w = item[0].top, item[0].bottom
diff = W-L
cnt = (diff+w-1)//w
ans = v*cnt
W -= w*cnt
print(ans+max(dp[:W+1]))
detteiuu