#include <iostream> #include <vector> #include <set> using namespace std; typedef long long int ll; int fish_count(multiset<ll>& w, ll s, ll k) { int res = 0; vector<ll> used; while (s < k) { auto upb = w.upper_bound(s-1); if (upb == w.begin()) { res = -1; break; } res++; upb--; ll val = *upb; s += val; w.erase(upb); used.push_back(val); } for (ll x : used) w.insert(x); return res; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; multiset<ll> w; ll wi; for (int i = 0; i < n; i++) { cin >> wi; w.insert(wi); } int q; cin >> q; while (q--) { int t; cin >> t; if (t == 1) { ll s, k; cin >> s >> k; cout << fish_count(w, s, k) << endl; } else { cin >> wi; if (t == 2) w.insert(wi); else { auto it = w.find(wi); w.erase(it); } } } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <iostream> #include <vector> #include <set> using namespace std; typedef long long int ll; int fish_count(multiset<ll>& w, ll s, ll k) { int res = 0; vector<ll> used; while (s < k) { auto upb = w.upper_bound(s-1); if (upb == w.begin()) { res = -1; break; } res++; upb--; ll val = *upb; s += val; w.erase(upb); used.push_back(val); } for (ll x : used) w.insert(x); return res; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; multiset<ll> w; ll wi; for (int i = 0; i < n; i++) { cin >> wi; w.insert(wi); } int q; cin >> q; while (q--) { int t; cin >> t; if (t == 1) { ll s, k; cin >> s >> k; cout << fish_count(w, s, k) << endl; } else { cin >> wi; if (t == 2) w.insert(wi); else { auto it = w.find(wi); w.erase(it); } } } return 0; } |