結果

問題 No.527 ナップサック容量問題
ユーザー t8m8⛄️t8m8⛄️
提出日時 2017-06-09 23:01:25
言語 Nim
(2.0.2)
結果
TLE  
実行時間 -
コード長 1,427 bytes
コンパイル時間 2,957 ms
コンパイル使用メモリ 68,500 KB
実行使用メモリ 23,252 KB
最終ジャッジ日時 2023-09-12 12:57:01
合計ジャッジ時間 7,106 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
12,864 KB
testcase_01 AC 29 ms
12,040 KB
testcase_02 AC 181 ms
23,252 KB
testcase_03 AC 30 ms
12,048 KB
testcase_04 AC 11 ms
8,320 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(3, 39) Warning: Use the new 'sugar' module instead; future is deprecated [Deprecated]
/home/judge/data/code/Main.nim(3, 28) Warning: imported and not used: 'algorithm' [UnusedImport]
/home/judge/data/code/Main.nim(3, 39) Warning: imported and not used: 'future' [UnusedImport]

ソースコード

diff #

{.warning[SmallLshouldNotBeUsed]: off.}

import strutils, sequtils, algorithm, future

proc f(v, w: seq[int], m, vm: int): bool =
  var
    n = v.len
    dp = newSeqWith(n+1, newSeq[int](m+1))
  for i in countDown(n-1, 0):
    for j in 0..m:
      if j < w[i]:
        dp[i][j] = dp[i+1][j]
      else:
        dp[i][j] = max(dp[i+1][j], dp[i+1][j-w[i]] + v[i])
  result = dp[0][m] < vm

proc g(v, w: seq[int], m, vm: int): bool =
  var
    n = v.len
    dp = newSeqWith(n+1, newSeq[int](m+1))
  for i in countDown(n-1, 0):
    for j in 0..m:
      if j < w[i]:
        dp[i][j] = dp[i+1][j]
      else:
        dp[i][j] = max(dp[i+1][j], dp[i+1][j-w[i]] + v[i])
  result = dp[0][m] <= vm

when isMainModule:
  var
    n = stdin.readLine.parseInt
    v = newSeq[int](n)
    w = newSeq[int](n)

  for i in 0..<n:
    var t = stdin.readLine.split.map(parseInt)
    (v[i], w[i]) = (t[0], t[1])

  var
    vm = stdin.readLine.parseInt
    (l, r) = (-1, 200000)

  while r - l > 1:
    var m = (l + r) div 2
    if f(v, w, m, vm): l = m
    else: r = m

  var a = -1
  for i in max(1, l-5)..l+5:
    if not f(v, w, i, vm):
      a = i
      break
  echo a

  (l, r) = (-1, 200000)
  while r - l > 1:
    var m = (l + r) div 2
    if g(v, w, m, vm): l = m
    else: r = m

  var b = l-5
  for i in max(1, l-5)..l+5:
    if g(v, w, i, vm):
      b = i
    else:
      break
  if not g(v, w, b+1, vm):
    echo b
  else:
    echo "inf"
0