結果

問題 No.527 ナップサック容量問題
ユーザー sekiya9311sekiya9311
提出日時 2017-06-09 23:17:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 639 ms / 2,000 ms
コード長 1,348 bytes
コンパイル時間 2,071 ms
コンパイル使用メモリ 167,540 KB
実行使用メモリ 47,096 KB
最終ジャッジ日時 2023-10-24 01:37:56
合計ジャッジ時間 15,786 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 230 ms
47,096 KB
testcase_01 AC 238 ms
47,096 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 228 ms
47,096 KB
testcase_04 AC 221 ms
47,096 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 483 ms
47,096 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 613 ms
47,096 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 346 ms
47,096 KB
testcase_15 AC 409 ms
47,096 KB
testcase_16 AC 242 ms
47,096 KB
testcase_17 AC 377 ms
47,096 KB
testcase_18 AC 424 ms
47,096 KB
testcase_19 AC 249 ms
47,096 KB
testcase_20 AC 349 ms
47,096 KB
testcase_21 AC 623 ms
47,096 KB
testcase_22 AC 462 ms
47,096 KB
testcase_23 AC 600 ms
47,096 KB
testcase_24 AC 293 ms
47,096 KB
testcase_25 AC 461 ms
47,096 KB
testcase_26 AC 418 ms
47,096 KB
testcase_27 AC 452 ms
47,096 KB
testcase_28 AC 402 ms
47,096 KB
testcase_29 AC 639 ms
47,096 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 370 ms
47,096 KB
testcase_32 AC 414 ms
47,096 KB
testcase_33 AC 383 ms
47,096 KB
testcase_34 AC 414 ms
47,096 KB
testcase_35 AC 431 ms
47,096 KB
testcase_36 AC 222 ms
47,096 KB
testcase_37 AC 354 ms
47,096 KB
testcase_38 AC 410 ms
47,096 KB
testcase_39 AC 404 ms
47,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

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


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