#include <cstdio> #include <set> using weight = long long; using pond = std::multiset<weight, std::greater<weight>>; int szczupak(weight s, weight k, pond& weights, pond& removed) { // printf("%lld->%lld RYBKI:", s, k); // for (weight w : weights) printf(" %lld", w); // putchar('\n'); int ile = 0; while (s < k) { auto it = weights.upper_bound(s); weight next = (it == weights.begin()) ? std::numeric_limits<weight>::max() : *std::prev(it); while (s < k && s <= next) { // printf("%lld: next=%lld\n", s, next); if (it == weights.end()) { return -1; } s += *it; removed.insert(*it); it = weights.erase(it); ++ile; } } return ile; } weight weights_array[300000]; int main() { int N; if (scanf("%d", &N) != 1) exit(1); weight weight_sum = 0; for (int i=0; i<N; ++i){ if (scanf("%lld", &weights_array[i]) != 1) exit(1); weight_sum += weights_array[i]; } pond weights(weights_array, weights_array+N); pond removed; int Q; if (scanf("%d", &Q) != 1) exit(1); for (int q=0; q<Q; ++q) { int typ; if (scanf("%d", &typ) != 1) exit(1); if (typ == 1) { weight s, k; if (scanf("%lld%lld", &s, &k) != 2) exit(1); int result; if (s + weight_sum < k) { result = -1; } else { result = szczupak(s, k, weights, removed); weights.merge(removed); } printf("%d\n", result); } else { weight w; if (scanf("%lld", &w) != 1) exit(1); if (typ == 2) { weights.insert(w); weight_sum += w; ++N; } else { weights.erase(w); weight_sum -= w; --N; } } } }
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 | #include <cstdio> #include <set> using weight = long long; using pond = std::multiset<weight, std::greater<weight>>; int szczupak(weight s, weight k, pond& weights, pond& removed) { // printf("%lld->%lld RYBKI:", s, k); // for (weight w : weights) printf(" %lld", w); // putchar('\n'); int ile = 0; while (s < k) { auto it = weights.upper_bound(s); weight next = (it == weights.begin()) ? std::numeric_limits<weight>::max() : *std::prev(it); while (s < k && s <= next) { // printf("%lld: next=%lld\n", s, next); if (it == weights.end()) { return -1; } s += *it; removed.insert(*it); it = weights.erase(it); ++ile; } } return ile; } weight weights_array[300000]; int main() { int N; if (scanf("%d", &N) != 1) exit(1); weight weight_sum = 0; for (int i=0; i<N; ++i){ if (scanf("%lld", &weights_array[i]) != 1) exit(1); weight_sum += weights_array[i]; } pond weights(weights_array, weights_array+N); pond removed; int Q; if (scanf("%d", &Q) != 1) exit(1); for (int q=0; q<Q; ++q) { int typ; if (scanf("%d", &typ) != 1) exit(1); if (typ == 1) { weight s, k; if (scanf("%lld%lld", &s, &k) != 2) exit(1); int result; if (s + weight_sum < k) { result = -1; } else { result = szczupak(s, k, weights, removed); weights.merge(removed); } printf("%d\n", result); } else { weight w; if (scanf("%lld", &w) != 1) exit(1); if (typ == 2) { weights.insert(w); weight_sum += w; ++N; } else { weights.erase(w); weight_sum -= w; --N; } } } } |