#include <iostream> #include <vector> #include <math.h> #include <algorithm> #include <stack> #include <map> using namespace std; #define vi vector<int> #define vb vector<bool> #define vvi vector<vi> #define vpii vector<pair<int, int>> #define str string #define vs vector<str> #define lli long long int #define vc vector<char> #define vvc vector<vc> void add_to_map(map<lli, int> &mp, lli a) { if (mp.count(a) == 0) mp[a] = 1; else mp[a]++; } void del_from_map(map<lli, int> &mp, lli a) { if (mp[a] == 1) mp.erase(a); else mp[a]--; } int eaten_fish(map<lli, int> &mp, stack<lli> &s, lli current, lli target) { if (current >= target) return 0; if (mp.empty()) return -1; map<lli, int>::iterator it = mp.lower_bound(current); if (it == mp.begin()) return -1; it--; current += it->first; s.push(it->first); del_from_map(mp, it->first); int ad = eaten_fish(mp, s, current, target); if (ad < 0) return -1; else return ad + 1; } void recover_map(map<lli, int> &mp, stack<lli> &s) { while (!s.empty()) { lli x = s.top(); s.pop(); add_to_map(mp, x); } } int main() { std::ios_base::sync_with_stdio(false); int n; cin >> n; map<lli, int> mp; lli sum = 0; for (int i = 0; i < n; i++) { lli w; cin >> w; if (mp.count(w) == 0) { mp[w] = 1; } else { mp[w]++; } sum += w; } stack<lli> s; int q; cin >> q; for (int i = 0; i < q; i++) { lli t, a, b; cin >> t; if (t == 2) { cin >> a; add_to_map(mp, a); sum += a; } else if (t == 3) { cin >> a; del_from_map(mp, a); sum -= a; } else { cin >> a >> b; int ans = eaten_fish(mp, s, a, b); recover_map(mp, s); cout << ans << endl; } } 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include <iostream> #include <vector> #include <math.h> #include <algorithm> #include <stack> #include <map> using namespace std; #define vi vector<int> #define vb vector<bool> #define vvi vector<vi> #define vpii vector<pair<int, int>> #define str string #define vs vector<str> #define lli long long int #define vc vector<char> #define vvc vector<vc> void add_to_map(map<lli, int> &mp, lli a) { if (mp.count(a) == 0) mp[a] = 1; else mp[a]++; } void del_from_map(map<lli, int> &mp, lli a) { if (mp[a] == 1) mp.erase(a); else mp[a]--; } int eaten_fish(map<lli, int> &mp, stack<lli> &s, lli current, lli target) { if (current >= target) return 0; if (mp.empty()) return -1; map<lli, int>::iterator it = mp.lower_bound(current); if (it == mp.begin()) return -1; it--; current += it->first; s.push(it->first); del_from_map(mp, it->first); int ad = eaten_fish(mp, s, current, target); if (ad < 0) return -1; else return ad + 1; } void recover_map(map<lli, int> &mp, stack<lli> &s) { while (!s.empty()) { lli x = s.top(); s.pop(); add_to_map(mp, x); } } int main() { std::ios_base::sync_with_stdio(false); int n; cin >> n; map<lli, int> mp; lli sum = 0; for (int i = 0; i < n; i++) { lli w; cin >> w; if (mp.count(w) == 0) { mp[w] = 1; } else { mp[w]++; } sum += w; } stack<lli> s; int q; cin >> q; for (int i = 0; i < q; i++) { lli t, a, b; cin >> t; if (t == 2) { cin >> a; add_to_map(mp, a); sum += a; } else if (t == 3) { cin >> a; del_from_map(mp, a); sum -= a; } else { cin >> a >> b; int ans = eaten_fish(mp, s, a, b); recover_map(mp, s); cout << ans << endl; } } return 0; } |