結果

問題 No.527 ナップサック容量問題
ユーザー t8m8⛄️t8m8⛄️
提出日時 2017-06-09 22:54:16
言語 Nim
(2.0.2)
結果
WA  
実行時間 -
コード長 1,406 bytes
コンパイル時間 3,042 ms
コンパイル使用メモリ 68,372 KB
実行使用メモリ 11,256 KB
最終ジャッジ日時 2023-09-12 12:56:53
合計ジャッジ時間 5,910 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 4 ms
4,384 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 3 ms
4,384 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 3 ms
4,380 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 5 ms
5,088 KB
testcase_20 AC 12 ms
7,392 KB
testcase_21 AC 29 ms
7,724 KB
testcase_22 AC 15 ms
11,256 KB
testcase_23 AC 14 ms
7,724 KB
testcase_24 AC 11 ms
7,144 KB
testcase_25 AC 6 ms
5,676 KB
testcase_26 AC 8 ms
6,264 KB
testcase_27 AC 8 ms
6,528 KB
testcase_28 AC 5 ms
5,184 KB
testcase_29 AC 10 ms
7,588 KB
testcase_30 AC 9 ms
6,764 KB
testcase_31 AC 5 ms
5,008 KB
testcase_32 AC 10 ms
6,868 KB
testcase_33 AC 8 ms
6,260 KB
testcase_34 AC 8 ms
5,612 KB
testcase_35 WA -
testcase_36 AC 2 ms
4,380 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
/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, 2001)

  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, 2001)
  while r - l > 1:
    var m = (l + r) div 2
    if g(v, w, m, vm): l = m
    else: r = m

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