結果
問題 | No.626 Randomized 01 Knapsack |
ユーザー |
![]() |
提出日時 | 2019-02-26 23:56:07 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
10,012 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 3 ms
6,944 KB |
testcase_08 | AC | 81 ms
6,944 KB |
testcase_09 | AC | 4 ms
6,940 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 | -- | - |
ソースコード
// 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; }