結果

問題 No.626 Randomized 01 Knapsack
ユーザー 0w10w1
提出日時 2017-12-26 14:52:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,311 bytes
コンパイル時間 2,933 ms
コンパイル使用メモリ 207,784 KB
実行使用メモリ 8,768 KB
最終ジャッジ日時 2023-08-22 21:46:04
合計ジャッジ時間 7,022 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 WA -
testcase_08 AC 510 ms
4,384 KB
testcase_09 WA -
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

long long branch_and_bound(int n, long long c, const std::vector<long long> &v, const std::vector<long long> &w) {
  std::vector<long long> vv(n), ww(n); {
    std::vector<int> id(n);
    std::iota(id.begin(), id.end(), 0);
    std::sort(id.begin(), id.end(), [&](int i, int j) {
      return 1.0 * v[i] * w[j] > 1.0 * v[j] * w[i];
    });
    for (int i = 0; i < n; ++i) {
      vv[i] = v[id[i]];
      ww[i] = w[id[i]];
    }
  }
  long long ans = 0;
  std::function<void(int, long long, long long)> dfs = [&](int x, long long sv, long long sw) {
    if (c < sw) return;
    if (x == n) return ans = std::max(ans, sv), void();
    long long gsv = sv, gsw = sw;
    int y = x;
    for ( ; y < n && gsw + w[y] <= c; ++y) {
      gsv += vv[y];
      gsw += ww[y];
    }
    if (gsw == c || y == n) return ans = std::max(ans, gsv), void();
    if (gsv + (c - gsw) * ((vv[y] + ww[y] - 1) / ww[y]) <= ans) return;
    dfs(x + 1, sv + vv[x], sw + ww[x]);
    dfs(x + 1, sv, sw);
  };
  dfs(0, 0, 0);
  return ans;
}

signed main() {
  std::ios::sync_with_stdio(false);
  int N;
  long long C;
  std::cin >> N >> C;
  std::vector<long long> V(N), W(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> V[i] >> W[i];
  }
  std::cout << branch_and_bound(N, C, V, W) << std::endl;
  return 0;
}
0