#include <bits/stdc++.h> using u32 = uint32_t; using u64 = uint64_t; using i32 = int32_t; using i64 = int64_t; int main() { i64 n; std::cin >> n; std::vector<i64> vec_w, vec_wt; i64 wx; for (i64 i = 0; i < n; i++) { std::cin >> wx; vec_wt.push_back(wx); } std::sort(vec_wt.begin(), vec_wt.end()); i64 q; i64 s; i64 k; std::cin >> q; for (i64 i = 0; i < q; i++) { vec_w = vec_wt; i64 type; std::cin >> type; i64 l(0); switch (type) { case 1: std::cin >> s >> k; while (s < k) { std::vector<i64>::iterator it = std::find_if(vec_w.begin(), vec_w.end(), [&](const auto &e) { return e >= s; }); if ((it == vec_w.end() and *(it - 1) >= s) or it == vec_w.begin()) { break; } l++; s += *(it - 1); vec_w.erase(it - 1); } if (s < k) { std::cout << -1 << std::endl; } else { std::cout << l << std::endl; } break; case 2: std::cin >> wx; vec_wt.push_back(wx); std::sort(vec_wt.begin(), vec_wt.end()); break; default: std::cin >> wx; std::vector<i64>::iterator it = std::find(vec_wt.begin(), vec_wt.end(), wx); if (it != vec_wt.end()) { vec_wt.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 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 <bits/stdc++.h> using u32 = uint32_t; using u64 = uint64_t; using i32 = int32_t; using i64 = int64_t; int main() { i64 n; std::cin >> n; std::vector<i64> vec_w, vec_wt; i64 wx; for (i64 i = 0; i < n; i++) { std::cin >> wx; vec_wt.push_back(wx); } std::sort(vec_wt.begin(), vec_wt.end()); i64 q; i64 s; i64 k; std::cin >> q; for (i64 i = 0; i < q; i++) { vec_w = vec_wt; i64 type; std::cin >> type; i64 l(0); switch (type) { case 1: std::cin >> s >> k; while (s < k) { std::vector<i64>::iterator it = std::find_if(vec_w.begin(), vec_w.end(), [&](const auto &e) { return e >= s; }); if ((it == vec_w.end() and *(it - 1) >= s) or it == vec_w.begin()) { break; } l++; s += *(it - 1); vec_w.erase(it - 1); } if (s < k) { std::cout << -1 << std::endl; } else { std::cout << l << std::endl; } break; case 2: std::cin >> wx; vec_wt.push_back(wx); std::sort(vec_wt.begin(), vec_wt.end()); break; default: std::cin >> wx; std::vector<i64>::iterator it = std::find(vec_wt.begin(), vec_wt.end(), wx); if (it != vec_wt.end()) { vec_wt.erase(it); } } } return 0; } |