#include <cstdio> #include <vector> #include <algorithm> #include <map> #include <queue> #include <iostream> using namespace std; struct Staw { map<unsigned long long, int> fish; Staw() { } void add_fish(unsigned long long w) { if (this->fish.find(w) == this->fish.end()) { this->fish[w] = 0; } this->fish[w]++; } void remove_fish(unsigned long long w) { this->fish[w]--; if (!this->fish[w]) { this->fish.erase(w); } } map<unsigned long long, int>::iterator get_fish_to_eat(unsigned long long s) { map<unsigned long long, int>::iterator fish_to_eat; fish_to_eat = this->fish.lower_bound(s); if (fish_to_eat != this->fish.begin()) { return --fish_to_eat; } return this->fish.end();; } int pike_attack(unsigned long long s, unsigned long long k) { Staw staw = Staw(); map<unsigned long long, int>::iterator fish_to_eat; int eats = 0; staw.fish = this->fish; while (s < k && fish_to_eat != staw.fish.end()) { fish_to_eat = staw.get_fish_to_eat(s); if (fish_to_eat != staw.fish.end()) { // printf("s: %llu eat: %llu = %llu\n", s, fish_to_eat->first, s+fish_to_eat->first); s += fish_to_eat->first; staw.remove_fish(fish_to_eat->first); eats++; } } return s >= k ? eats : -1; } void print() { for(map<unsigned long long, int>::iterator it=this->fish.begin(); it != this->fish.end(); it++) { printf("%llu: %d\n", it->first, it->second); } } }; int main() { int n, q; Staw staw = Staw(); scanf("%d", &n); for(int i=0; i<n; i++) { unsigned long long w; scanf("%llu", &w); staw.add_fish(w); } scanf("%d", &q); for(int i=0; i<q; i++) { unsigned long long s, k, w; int type; scanf("%d", &type); if (type == 1) { scanf("%llu %llu", &s, &k); printf("%d\n", staw.pike_attack(s, k)); } else if (type == 2) { scanf("%llu", &w); staw.add_fish(w); } else { scanf("%llu", &w); staw.remove_fish(w); } } }
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 | #include <cstdio> #include <vector> #include <algorithm> #include <map> #include <queue> #include <iostream> using namespace std; struct Staw { map<unsigned long long, int> fish; Staw() { } void add_fish(unsigned long long w) { if (this->fish.find(w) == this->fish.end()) { this->fish[w] = 0; } this->fish[w]++; } void remove_fish(unsigned long long w) { this->fish[w]--; if (!this->fish[w]) { this->fish.erase(w); } } map<unsigned long long, int>::iterator get_fish_to_eat(unsigned long long s) { map<unsigned long long, int>::iterator fish_to_eat; fish_to_eat = this->fish.lower_bound(s); if (fish_to_eat != this->fish.begin()) { return --fish_to_eat; } return this->fish.end();; } int pike_attack(unsigned long long s, unsigned long long k) { Staw staw = Staw(); map<unsigned long long, int>::iterator fish_to_eat; int eats = 0; staw.fish = this->fish; while (s < k && fish_to_eat != staw.fish.end()) { fish_to_eat = staw.get_fish_to_eat(s); if (fish_to_eat != staw.fish.end()) { // printf("s: %llu eat: %llu = %llu\n", s, fish_to_eat->first, s+fish_to_eat->first); s += fish_to_eat->first; staw.remove_fish(fish_to_eat->first); eats++; } } return s >= k ? eats : -1; } void print() { for(map<unsigned long long, int>::iterator it=this->fish.begin(); it != this->fish.end(); it++) { printf("%llu: %d\n", it->first, it->second); } } }; int main() { int n, q; Staw staw = Staw(); scanf("%d", &n); for(int i=0; i<n; i++) { unsigned long long w; scanf("%llu", &w); staw.add_fish(w); } scanf("%d", &q); for(int i=0; i<q; i++) { unsigned long long s, k, w; int type; scanf("%d", &type); if (type == 1) { scanf("%llu %llu", &s, &k); printf("%d\n", staw.pike_attack(s, k)); } else if (type == 2) { scanf("%llu", &w); staw.add_fish(w); } else { scanf("%llu", &w); staw.remove_fish(w); } } } |