#include <cstdio> #include <vector> #include <algorithm> #include <stack> typedef long long ll; int TryEatFish(std::vector<ll>* prey) { ll size, target; int eaten_fish = 0; scanf("%lld %lld", &size, &target); ll* fish_to_eat = new ll[prey->size()]; ll top_fish = -1; auto smaller_fish = prey->begin(); while (size < target) { while (smaller_fish != prey->end() && *smaller_fish < size) { top_fish++; fish_to_eat[top_fish] = *smaller_fish; smaller_fish++; } if (top_fish == -1) { return -1; } size += fish_to_eat[top_fish]; eaten_fish++; top_fish--; } delete[] fish_to_eat; return eaten_fish; } void AddFish(std::vector<ll>* prey) { ll new_fish; scanf("%lld", &new_fish); auto lower_bound = prey->begin(); while (lower_bound != prey->end() && *lower_bound < new_fish) { lower_bound++; } if (lower_bound == prey->end()) { prey->push_back(new_fish); } else { prey->insert(lower_bound, new_fish); } } void RemoveFish(std::vector<ll>* prey) { ll target_fish; scanf("%lld", &target_fish); auto it = prey->begin(); while (*it != target_fish) { it++; } prey->erase(it); } int main() { int n; scanf("%d", &n); std::vector<ll> prey; for (int i = 0; i < n; i++) { ll fish; scanf("%lld", &fish); prey.push_back(fish); } std::sort(prey.begin(), prey.end()); int commands; scanf("%d", &commands); for (int i = 0; i < commands; i++) { int type; scanf("%d", &type); switch (type) { case 1:printf("%d\n", TryEatFish(&prey)); break; case 2:AddFish(&prey); break; default:RemoveFish(&prey); } } 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 82 83 84 85 86 87 | #include <cstdio> #include <vector> #include <algorithm> #include <stack> typedef long long ll; int TryEatFish(std::vector<ll>* prey) { ll size, target; int eaten_fish = 0; scanf("%lld %lld", &size, &target); ll* fish_to_eat = new ll[prey->size()]; ll top_fish = -1; auto smaller_fish = prey->begin(); while (size < target) { while (smaller_fish != prey->end() && *smaller_fish < size) { top_fish++; fish_to_eat[top_fish] = *smaller_fish; smaller_fish++; } if (top_fish == -1) { return -1; } size += fish_to_eat[top_fish]; eaten_fish++; top_fish--; } delete[] fish_to_eat; return eaten_fish; } void AddFish(std::vector<ll>* prey) { ll new_fish; scanf("%lld", &new_fish); auto lower_bound = prey->begin(); while (lower_bound != prey->end() && *lower_bound < new_fish) { lower_bound++; } if (lower_bound == prey->end()) { prey->push_back(new_fish); } else { prey->insert(lower_bound, new_fish); } } void RemoveFish(std::vector<ll>* prey) { ll target_fish; scanf("%lld", &target_fish); auto it = prey->begin(); while (*it != target_fish) { it++; } prey->erase(it); } int main() { int n; scanf("%d", &n); std::vector<ll> prey; for (int i = 0; i < n; i++) { ll fish; scanf("%lld", &fish); prey.push_back(fish); } std::sort(prey.begin(), prey.end()); int commands; scanf("%d", &commands); for (int i = 0; i < commands; i++) { int type; scanf("%d", &type); switch (type) { case 1:printf("%d\n", TryEatFish(&prey)); break; case 2:AddFish(&prey); break; default:RemoveFish(&prey); } } return 0; } |