#include <cstdio> #include <vector> #include <set> using namespace std; int solve(long long s, long long k, multiset<long long>& set, vector<long long>& erased) { int num = 0; while (s < k) { auto it = set.lower_bound(s); if (!set.empty() && it != set.begin()) { it--; s += *it; erased.push_back(*it); set.erase(it); num++; } else { return -1; } } return num; } int main() { int i, n, q, t; long long w, s, k; multiset<long long> set; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%lld", &w); set.insert(w); } scanf("%d", &q); for (i = 0; i < q; ++i) { scanf("%d", &t); if (t == 1) { scanf("%lld%lld", &s, &k); vector<long long> erased; printf("%d\n", solve(s, k, set, erased)); set.insert(erased.begin(), erased.end()); } else if (t == 2) { scanf("%lld", &w); set.insert(w); } else if (t == 3) { scanf("%lld", &w); auto it = set.find(w); set.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 | #include <cstdio> #include <vector> #include <set> using namespace std; int solve(long long s, long long k, multiset<long long>& set, vector<long long>& erased) { int num = 0; while (s < k) { auto it = set.lower_bound(s); if (!set.empty() && it != set.begin()) { it--; s += *it; erased.push_back(*it); set.erase(it); num++; } else { return -1; } } return num; } int main() { int i, n, q, t; long long w, s, k; multiset<long long> set; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%lld", &w); set.insert(w); } scanf("%d", &q); for (i = 0; i < q; ++i) { scanf("%d", &t); if (t == 1) { scanf("%lld%lld", &s, &k); vector<long long> erased; printf("%d\n", solve(s, k, set, erased)); set.insert(erased.begin(), erased.end()); } else if (t == 2) { scanf("%lld", &w); set.insert(w); } else if (t == 3) { scanf("%lld", &w); auto it = set.find(w); set.erase(it); } } return 0; } |