#include<cstdio> #include<iostream> #include<queue> #include<map> using namespace std; map<long long int, int> fish; int main() { ios_base::sync_with_stdio(0); long long int n, k, a, type; long long int has, wants, eats; cin >> n; for(int i = 1; i <= n; i++) { cin >> a; fish[a] += 1; } cin >> k; for(int i = 1; i <= k; i++) { cin >> type; if(type == 1) { cin >> has >> wants; if(has >= wants) { cout << "0\n"; } else { map<long long int,int> fish2(fish); eats = 0; while(has < wants) { map<long long int, int>::iterator it = fish2.lower_bound(has); if(it == fish2.begin()) { eats = -1; break; } else { it--; has = has + it->first; it->second -= 1; if(it->second == 0) { fish2.erase(it); } eats += 1; } } cout << eats << endl; } } else if(type == 2) { cin >> a; fish[a] += 1; } else { cin >> a; fish[a] -= 1; if(fish[a] == 0) { fish.erase(a); } } } 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 | #include<cstdio> #include<iostream> #include<queue> #include<map> using namespace std; map<long long int, int> fish; int main() { ios_base::sync_with_stdio(0); long long int n, k, a, type; long long int has, wants, eats; cin >> n; for(int i = 1; i <= n; i++) { cin >> a; fish[a] += 1; } cin >> k; for(int i = 1; i <= k; i++) { cin >> type; if(type == 1) { cin >> has >> wants; if(has >= wants) { cout << "0\n"; } else { map<long long int,int> fish2(fish); eats = 0; while(has < wants) { map<long long int, int>::iterator it = fish2.lower_bound(has); if(it == fish2.begin()) { eats = -1; break; } else { it--; has = has + it->first; it->second -= 1; if(it->second == 0) { fish2.erase(it); } eats += 1; } } cout << eats << endl; } } else if(type == 2) { cin >> a; fish[a] += 1; } else { cin >> a; fish[a] -= 1; if(fish[a] == 0) { fish.erase(a); } } } return 0; } |