#include <iostream> #include <algorithm> #include <set> // const MAXSZPROTKI = 300000 + 100000; std::multiset<long> szprotki; std::pair<std::multiset<long>::iterator, std::multiset<long>::iterator> get_food_target_iterators(long weight, std::multiset<long>& szprotki_copy) { auto current_target = szprotki_copy.lower_bound(weight); auto current_food = current_target; if (current_food == szprotki_copy.end() && current_food != szprotki_copy.begin()) { --current_food; } while (current_food != szprotki_copy.begin() && (*current_food) >= weight){ --current_food; } return {current_food, current_target}; } long szczupak(){ long eaten = 0; long weight, target; std::cin >> weight >> target; if (weight == target) { return 0; } if (szprotki.size() == 0) { return -1; } std::multiset<long> szprotki_copy = szprotki; auto iters = get_food_target_iterators(weight, szprotki_copy); auto current_food = iters.first; auto current_target = iters.second; while (weight < target) { ++eaten; bool end_of_food; auto next_food = current_food; if (current_food == szprotki_copy.begin()) { end_of_food = true; } else { end_of_food = false; --next_food; } long food = *current_food; szprotki_copy.erase(current_food); weight += food; if (current_target != szprotki_copy.end() && (*current_target) < weight) { iters = get_food_target_iterators(weight, szprotki_copy); current_food = iters.first; current_target = iters.second; } else{ if (weight < target) { if (end_of_food) { return -1; } else { current_food = next_food; } } } } return eaten; } void szprotkaplus(){ long szprotka; std::cin >> szprotka; szprotki.insert(szprotka); } void szprotkaminus(){ long szprotka; std::cin >> szprotka; szprotki.erase(szprotki.find(szprotka)); } int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; for (int i=0; i<n; ++i) { long w; std::cin >> w; szprotki.insert(w); } int q; std::cin >> q; for (int i=0; i < q; ++i) { int event; std::cin >> event; if (event == 1){ long result = szczupak(); std::cout << result << std::endl; } else if (event == 2){ szprotkaplus(); } else { szprotkaminus(); } } }
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include <iostream> #include <algorithm> #include <set> // const MAXSZPROTKI = 300000 + 100000; std::multiset<long> szprotki; std::pair<std::multiset<long>::iterator, std::multiset<long>::iterator> get_food_target_iterators(long weight, std::multiset<long>& szprotki_copy) { auto current_target = szprotki_copy.lower_bound(weight); auto current_food = current_target; if (current_food == szprotki_copy.end() && current_food != szprotki_copy.begin()) { --current_food; } while (current_food != szprotki_copy.begin() && (*current_food) >= weight){ --current_food; } return {current_food, current_target}; } long szczupak(){ long eaten = 0; long weight, target; std::cin >> weight >> target; if (weight == target) { return 0; } if (szprotki.size() == 0) { return -1; } std::multiset<long> szprotki_copy = szprotki; auto iters = get_food_target_iterators(weight, szprotki_copy); auto current_food = iters.first; auto current_target = iters.second; while (weight < target) { ++eaten; bool end_of_food; auto next_food = current_food; if (current_food == szprotki_copy.begin()) { end_of_food = true; } else { end_of_food = false; --next_food; } long food = *current_food; szprotki_copy.erase(current_food); weight += food; if (current_target != szprotki_copy.end() && (*current_target) < weight) { iters = get_food_target_iterators(weight, szprotki_copy); current_food = iters.first; current_target = iters.second; } else{ if (weight < target) { if (end_of_food) { return -1; } else { current_food = next_food; } } } } return eaten; } void szprotkaplus(){ long szprotka; std::cin >> szprotka; szprotki.insert(szprotka); } void szprotkaminus(){ long szprotka; std::cin >> szprotka; szprotki.erase(szprotki.find(szprotka)); } int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; for (int i=0; i<n; ++i) { long w; std::cin >> w; szprotki.insert(w); } int q; std::cin >> q; for (int i=0; i < q; ++i) { int event; std::cin >> event; if (event == 1){ long result = szczupak(); std::cout << result << std::endl; } else if (event == 2){ szprotkaplus(); } else { szprotkaminus(); } } } |