#include <bits/stdc++.h> using namespace std; typedef intmax_t I; constexpr I inf = 1e18; typedef pair<I, I> II; typedef double F; #define debug_printable(template_thingies, type, start, printer, stop) \ template <template_thingies> \ ostream &operator<<(ostream &o, const type &v) { \ for (auto i = (o << start, v.begin()); i != v.end(); ++i) \ (i != v.begin() && (o << ", ")), o << printer; \ return o << stop; \ } #define comma , debug_printable(typename T, vector<T>, "[", *i, "]"); debug_printable(typename T comma size_t N, array<T comma N>, "<", *i, ">"); debug_printable(typename T, set<T>, "{", *i, "}"); debug_printable(typename T comma typename U, map<T comma U>, "{", i->first << ": " << i->second, "}"); template <typename T, typename U> ostream &operator<<(ostream &o, pair<T, U> p) { return o << "(" << p.first << ", " << p.second << ")"; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); I n; cin >> n; map<I, I> v; for (I i = 0; i < n; ++i) { I x; cin >> x; ++v[x]; } auto solve = [&](I s, I k) { auto w = v; // cout << w << " " << s << " " << k << "\n"; I r = 0; while (s < k) { auto j = w.lower_bound(s); if (j == w.begin()) return (I)-1; auto i = prev(j); I c = 1; r += c; s += i->first * c; i->second -= c; if (i->second == 0) w.erase(i); } // cout << w << " -> " << r << endl; return r; }; I q; cin >> q; while (q--) { I o; cin >> o; if (o == 1) { I s, k; cin >> s >> k; cout << solve(s, k) << "\n"; } else { I x; cin >> x; if (o == 2) { ++v[x]; } else if (o == 3) { auto i = v.find(x); --i->second; if (i->second == 0) v.erase(i); } } } #ifdef UNITEST cout.flush(), system("pause"); #endif }
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #include <bits/stdc++.h> using namespace std; typedef intmax_t I; constexpr I inf = 1e18; typedef pair<I, I> II; typedef double F; #define debug_printable(template_thingies, type, start, printer, stop) \ template <template_thingies> \ ostream &operator<<(ostream &o, const type &v) { \ for (auto i = (o << start, v.begin()); i != v.end(); ++i) \ (i != v.begin() && (o << ", ")), o << printer; \ return o << stop; \ } #define comma , debug_printable(typename T, vector<T>, "[", *i, "]"); debug_printable(typename T comma size_t N, array<T comma N>, "<", *i, ">"); debug_printable(typename T, set<T>, "{", *i, "}"); debug_printable(typename T comma typename U, map<T comma U>, "{", i->first << ": " << i->second, "}"); template <typename T, typename U> ostream &operator<<(ostream &o, pair<T, U> p) { return o << "(" << p.first << ", " << p.second << ")"; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); I n; cin >> n; map<I, I> v; for (I i = 0; i < n; ++i) { I x; cin >> x; ++v[x]; } auto solve = [&](I s, I k) { auto w = v; // cout << w << " " << s << " " << k << "\n"; I r = 0; while (s < k) { auto j = w.lower_bound(s); if (j == w.begin()) return (I)-1; auto i = prev(j); I c = 1; r += c; s += i->first * c; i->second -= c; if (i->second == 0) w.erase(i); } // cout << w << " -> " << r << endl; return r; }; I q; cin >> q; while (q--) { I o; cin >> o; if (o == 1) { I s, k; cin >> s >> k; cout << solve(s, k) << "\n"; } else { I x; cin >> x; if (o == 2) { ++v[x]; } else if (o == 3) { auto i = v.find(x); --i->second; if (i->second == 0) v.erase(i); } } } #ifdef UNITEST cout.flush(), system("pause"); #endif } |