#include <iostream> #include <vector> #include <algorithm> int main() { int loop_iterations; int temp; std::vector<unsigned long>x; std::cin >> loop_iterations; for (int i = 0; i < loop_iterations; ++i) { std::cin >> temp; x.insert(x.end(), temp); } std::sort(x.begin(), x.end()); std::cin >> loop_iterations; for (int i = 0; i < loop_iterations; ++i) { std::cin >> temp; if (temp == 1) { int eats = 0; unsigned long source_weight; unsigned long destination_weight; std::cin >> source_weight; std::cin >> destination_weight; bool good = true; bool reset = false; std::vector<unsigned long> y = x; while (source_weight < destination_weight) { bool changed = false; for (std::vector<unsigned long>::reverse_iterator it = y.rbegin(); it != y.rend(); ++it) { if (*it < source_weight) { source_weight += *it; y.erase(--it.base()); ++eats; changed = true; break; } } if (!changed) { good = false; break; } } std::cout << (good ? eats : -1)<<"\n"; } else if (temp == 2) { std::cin >> temp; std::vector<unsigned long>::iterator xd; for (xd = x.begin(); xd != x.end() && *xd < temp; ++xd); x.insert(xd, temp); } else { std::cin >> temp; auto xd = std::find(x.begin(), x.end(), temp); if (xd != x.end()) { x.erase(xd); } } } }
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 | #include <iostream> #include <vector> #include <algorithm> int main() { int loop_iterations; int temp; std::vector<unsigned long>x; std::cin >> loop_iterations; for (int i = 0; i < loop_iterations; ++i) { std::cin >> temp; x.insert(x.end(), temp); } std::sort(x.begin(), x.end()); std::cin >> loop_iterations; for (int i = 0; i < loop_iterations; ++i) { std::cin >> temp; if (temp == 1) { int eats = 0; unsigned long source_weight; unsigned long destination_weight; std::cin >> source_weight; std::cin >> destination_weight; bool good = true; bool reset = false; std::vector<unsigned long> y = x; while (source_weight < destination_weight) { bool changed = false; for (std::vector<unsigned long>::reverse_iterator it = y.rbegin(); it != y.rend(); ++it) { if (*it < source_weight) { source_weight += *it; y.erase(--it.base()); ++eats; changed = true; break; } } if (!changed) { good = false; break; } } std::cout << (good ? eats : -1)<<"\n"; } else if (temp == 2) { std::cin >> temp; std::vector<unsigned long>::iterator xd; for (xd = x.begin(); xd != x.end() && *xd < temp; ++xd); x.insert(xd, temp); } else { std::cin >> temp; auto xd = std::find(x.begin(), x.end(), temp); if (xd != x.end()) { x.erase(xd); } } } } |