結果

問題 No.527 ナップサック容量問題
ユーザー sekiya9311sekiya9311
提出日時 2017-06-09 23:29:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,240 bytes
コンパイル時間 1,596 ms
コンパイル使用メモリ 167,484 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 02:21:43
合計ジャッジ時間 7,058 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
4,348 KB
testcase_01 AC 17 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 16 ms
4,348 KB
testcase_04 AC 8 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 207 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 300 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 101 ms
4,348 KB
testcase_15 AC 151 ms
4,348 KB
testcase_16 AC 17 ms
4,348 KB
testcase_17 AC 129 ms
4,348 KB
testcase_18 AC 154 ms
4,348 KB
testcase_19 AC 20 ms
4,348 KB
testcase_20 AC 98 ms
4,348 KB
testcase_21 AC 282 ms
4,348 KB
testcase_22 AC 186 ms
4,348 KB
testcase_23 AC 270 ms
4,348 KB
testcase_24 AC 57 ms
4,348 KB
testcase_25 AC 180 ms
4,348 KB
testcase_26 AC 157 ms
4,348 KB
testcase_27 AC 169 ms
4,348 KB
testcase_28 AC 142 ms
4,348 KB
testcase_29 AC 286 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 115 ms
4,348 KB
testcase_32 AC 145 ms
4,348 KB
testcase_33 AC 125 ms
4,348 KB
testcase_34 AC 145 ms
4,348 KB
testcase_35 AC 172 ms
4,348 KB
testcase_36 AC 14 ms
4,348 KB
testcase_37 AC 114 ms
4,348 KB
testcase_38 AC 151 ms
4,348 KB
testcase_39 AC 149 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int MAX = 111;
int N, V;
int v[MAX], w[MAX];

int dp[(int) 1e5 + 10];
// (i, j) = j:重さ


bool check(const int W, bool f) {
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 1e5 + 9; j >= 0; j--) {
      if (dp[j] != -1 && j + w[i] <= W) {
        dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]);
      }
    }
  }
  int ma = 0;
  for (int i = 0; i < 1e5 + 10; i++) {
    if (dp[i] != -1) {
      ma = max(ma, dp[i]);
    }
  }
  if (f) return ma <= V;
  else return ma < V;
}

int main() {
  scanf("%d", &N);
  long long sum = 0, sumw = 0;;
  for (int i = 0; i < N; i++) {
    scanf("%d%d", v + i, w + i);
    sum += v[i];
    sumw += w[i];
  }
  scanf("%d", &V);
  if (sum <= V) {
    cout << sumw << endl;
    cout << "inf" << endl;
    return 0;
  }
  int high = 1e5, low = 1;
  while (high - low > 1) {
    int mid = (high + low) / 2;
    if (check(mid, false)) low = mid;
    else high = mid;
  }
  int mi = low + check(low, false);
  high = 1e5, low = 1;
  while (high - low > 1) {
    int mid = (high + low) / 2;
    if (check(mid, true)) low = mid;
    else high = mid;
  }
  cout << mi << endl;
  cout << low << endl;
  return 0;
}
0