結果

問題 No.527 ナップサック容量問題
ユーザー t8m8⛄️t8m8⛄️
提出日時 2017-06-09 23:08:58
言語 Nim
(2.0.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 1,205 bytes
コンパイル時間 3,135 ms
コンパイル使用メモリ 68,636 KB
実行使用メモリ 83,436 KB
最終ジャッジ日時 2023-09-12 12:57:10
合計ジャッジ時間 7,838 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
6,132 KB
testcase_01 AC 11 ms
7,048 KB
testcase_02 AC 6 ms
5,396 KB
testcase_03 AC 11 ms
6,900 KB
testcase_04 AC 4 ms
4,564 KB
testcase_05 AC 78 ms
35,292 KB
testcase_06 AC 156 ms
71,284 KB
testcase_07 AC 126 ms
57,728 KB
testcase_08 AC 68 ms
30,936 KB
testcase_09 AC 4 ms
4,556 KB
testcase_10 AC 158 ms
71,940 KB
testcase_11 AC 113 ms
52,104 KB
testcase_12 AC 83 ms
38,320 KB
testcase_13 AC 160 ms
73,936 KB
testcase_14 AC 62 ms
28,264 KB
testcase_15 AC 95 ms
43,512 KB
testcase_16 AC 11 ms
6,920 KB
testcase_17 AC 79 ms
36,552 KB
testcase_18 AC 90 ms
42,728 KB
testcase_19 AC 13 ms
7,652 KB
testcase_20 AC 67 ms
29,900 KB
testcase_21 AC 183 ms
83,436 KB
testcase_22 AC 123 ms
57,348 KB
testcase_23 AC 176 ms
81,240 KB
testcase_24 AC 40 ms
19,852 KB
testcase_25 AC 120 ms
54,128 KB
testcase_26 AC 107 ms
49,348 KB
testcase_27 AC 108 ms
50,592 KB
testcase_28 AC 96 ms
43,136 KB
testcase_29 AC 179 ms
83,044 KB
testcase_30 AC 21 ms
10,808 KB
testcase_31 AC 82 ms
36,992 KB
testcase_32 AC 99 ms
45,648 KB
testcase_33 AC 85 ms
39,828 KB
testcase_34 AC 103 ms
47,020 KB
testcase_35 AC 102 ms
46,820 KB
testcase_36 AC 8 ms
6,084 KB
testcase_37 AC 78 ms
35,556 KB
testcase_38 AC 97 ms
43,924 KB
testcase_39 AC 90 ms
42,168 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/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, 39) Warning: imported and not used: 'future' [UnusedImport]
/home/judge/data/code/Main.nim(3, 28) Warning: imported and not used: 'algorithm' [UnusedImport]

ソースコード

diff #

{.warning[SmallLshouldNotBeUsed]: off.}

import strutils, sequtils, algorithm, future

proc f(m, vm: int, dp: seq[seq[int]]): bool =
  result = dp[0][m] < vm

proc g(m, vm: int, dp: seq[seq[int]]): bool =
  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

  var
    W = 100001
    dp = newSeqWith(n+1, newSeq[int](W+1))
  for i in countDown(n-1, 0):
    for j in 0..W:
      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])

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

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

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

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