結果

問題 No.626 Randomized 01 Knapsack
ユーザー lumc_lumc_
提出日時 2019-02-26 23:56:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,313 bytes
コンパイル時間 1,275 ms
コンパイル使用メモリ 106,032 KB
実行使用メモリ 7,688 KB
最終ジャッジ日時 2023-09-05 09:04:29
合計ジャッジ時間 6,581 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,664 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,500 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 98 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
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 #

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
// #include<deque>
// #include<multiset>
// #include<bitset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

int n, W;
constexpr int N = 5000;
ll weight[N], value[N];
vector<int> ord;

ll global_best;

// こんな感じですか.わかりません

void dfs(int id, ll value_sum, ll weight_rest) {
  global_best = max(global_best, value_sum);
  if(id == n) return;
  int i = ord[id];
  double now_upper_bound = value_sum + weight_rest * (double) value[i] / weight[i];
  if(now_upper_bound < global_best) return;
  // drop or adopt
  if(weight_rest >= weight[i]) dfs(id + 1, value_sum + value[i], weight_rest - weight[i]);
  dfs(id + 1, value_sum, weight_rest);
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> W;
  for(int i = 0; i < n; i++) cin >> value[i] >> weight[i];
  ord.resize(n);
  iota(begin(ord), end(ord), 0);
  sort(begin(ord), end(ord), [&](int a, int b) {
      return (double) value[a] / weight[a] > (double) value[b] / weight[b];
      });
  dfs(0, 0, W);
  cout << global_best << endl;
  return 0;
}
0