結果

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

テストケース

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

// こうですか.そうですよね
// 0 <= z <= 1 を緩和するんですもんね
// EPSの変化やDFSの探索順序,DFSをBFSに変えたらどうなるかなども気になるところです

int n, W;
constexpr int N = 5000;
vector<pair<ll, ll>> objs;
ll weight_sum[N];
ll value_sum[N];

ll global_best;

constexpr double EPS = 1e-18;

void dfs(int i, ll val, ll weight_rest) {
  global_best = max(global_best, val);
  if(i == n) return;
  int ok = i - 1, ng = n;
  while(ng - ok > 1) {
    int mid = (ng + ok) >> 1;
    ll sum = weight_sum[mid];
    if(i - 1 >= 0) sum -= weight_sum[i - 1];
    if(sum <= weight_rest) ok = mid; else ng = mid;
  }
  ll sum = 0;
  if(ok >= 0) sum += weight_sum[ok];
  if(i - 1 >= 0) sum -= weight_sum[i - 1];

  ll vsum = 0;
  if(ok >= 0) vsum += value_sum[ok];
  if(i - 1 >= 0) vsum -= value_sum[i - 1];

  double now_upper_bound = val + vsum;
  if(ok + 1 < n) now_upper_bound += (double) objs[ok + 1].first / objs[ok + 1].second * weight_rest;

  if(floor(now_upper_bound + EPS) < global_best) return;

  // drop or adopt
  if(weight_rest >= objs[i].second) dfs(i + 1, val + objs[i].first, weight_rest - objs[i].second);
  dfs(i + 1, val, weight_rest);
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> W;
  objs.resize(n);
  for(int i = 0; i < n; i++) cin >> objs[i].first >> objs[i].second;
  sort(begin(objs), end(objs), [&](const pair<ll, ll> &a, const pair<ll, ll> &b) {
      return (double) a.first / a.second > (double) b.first / b.second;
      });
  value_sum[0] = objs[0].first;
  for(int i = 1; i < n; i++) value_sum[i] = value_sum[i-1] + objs[i].first;
  weight_sum[0] = objs[0].second;
  for(int i = 1; i < n; i++) weight_sum[i] = weight_sum[i-1] + objs[i].second;
  dfs(0, 0, W);
  cout << global_best << endl;
  return 0;
}
0