結果

問題 No.626 Randomized 01 Knapsack
ユーザー lumc_
提出日時 2019-02-26 23:56:07
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,313 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 108,136 KB
実行使用メモリ 14,008 KB
最終ジャッジ日時 2024-06-23 04:57:05
合計ジャッジ時間 5,095 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 TLE * 1 -- * 14
権限があれば一括ダウンロードができます

ソースコード

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