結果

問題 No.527 ナップサック容量問題
ユーザー simansiman
提出日時 2022-10-27 16:49:51
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 337 ms / 2,000 ms
コード長 1,273 bytes
コンパイル時間 1,151 ms
コンパイル使用メモリ 104,156 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 10:54:53
合計ジャッジ時間 4,206 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 8 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 132 ms
4,376 KB
testcase_06 AC 320 ms
4,376 KB
testcase_07 AC 49 ms
4,380 KB
testcase_08 AC 110 ms
4,380 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 120 ms
4,376 KB
testcase_11 AC 202 ms
4,376 KB
testcase_12 AC 141 ms
4,380 KB
testcase_13 AC 337 ms
4,376 KB
testcase_14 AC 25 ms
4,384 KB
testcase_15 AC 31 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 36 ms
4,380 KB
testcase_18 AC 47 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 14 ms
4,380 KB
testcase_21 AC 44 ms
4,380 KB
testcase_22 AC 28 ms
4,376 KB
testcase_23 AC 41 ms
4,376 KB
testcase_24 AC 9 ms
4,380 KB
testcase_25 AC 27 ms
4,376 KB
testcase_26 AC 24 ms
4,376 KB
testcase_27 AC 25 ms
4,380 KB
testcase_28 AC 21 ms
4,380 KB
testcase_29 AC 44 ms
4,380 KB
testcase_30 AC 27 ms
4,376 KB
testcase_31 AC 15 ms
4,380 KB
testcase_32 AC 18 ms
4,380 KB
testcase_33 AC 15 ms
4,376 KB
testcase_34 AC 18 ms
4,376 KB
testcase_35 AC 54 ms
4,376 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 20 ms
4,376 KB
testcase_38 AC 31 ms
4,380 KB
testcase_39 AC 45 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int N;
vector<int> V;
vector<int> W;

int solve(int lim) {
  int dp[lim + 1];
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  int ans = 0;

  for (int i = 0; i < N; ++i) {
    int v = V[i];
    int w = W[i];

    for (int j = lim - w; j >= 0; --j) {
      if (dp[j] == -1) continue;

      int nj = j + w;
      dp[nj] = max(dp[nj], dp[j] + v);
      ans = max(ans, dp[nj]);
    }
  }

  return ans;
}

int main() {
  cin >> N;

  V.resize(N);
  W.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> V[i] >> W[i];
  }

  int V;
  cin >> V;

  int ng = 0;
  int ok = 200000;

  while (abs(ok - ng) >= 2) {
    int x = (ok + ng) / 2;
    int v = solve(x);

    if (v >= V) {
      ok = x;
    } else {
      ng = x;
    }
  }

  cout << ok << endl;

  ng = 0;
  ok = 200000;

  while (abs(ok - ng) >= 2) {
    int x = (ok + ng) / 2;
    int v = solve(x);

    if (v > V) {
      ok = x;
    } else {
      ng = x;
    }
  }

  if (ok <= 100000) {
    cout << ok - 1 << endl;
  } else {
    cout << "inf" << endl;
  }

  return 0;
}
0