#include<bits/stdc++.h> #define int long long using namespace std; multiset<int> a; int solve(int x, int y) { int res = 0; vector<int> backup; while(x < y) { auto it = a.upper_bound(-x); if(it == a.end()) { for(auto asd : backup) a.insert(-asd); return -1; } backup.emplace_back(-*it); x += -*it; a.erase(it); ++res; } for(auto asd : backup) a.insert(-asd); return res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int tmp; for(int i=0; i<n; ++i) cin >> tmp, a.insert(-tmp); int q, x, y; cin >> q; while(q--) { int t; cin >> t; if(t == 1) { cin >> x >> y; cout << solve(x,y) << '\n'; } else if(t == 2) { cin >> x; a.insert(-x); } else { cin >> x; auto it = a.find(-x); a.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 | #include<bits/stdc++.h> #define int long long using namespace std; multiset<int> a; int solve(int x, int y) { int res = 0; vector<int> backup; while(x < y) { auto it = a.upper_bound(-x); if(it == a.end()) { for(auto asd : backup) a.insert(-asd); return -1; } backup.emplace_back(-*it); x += -*it; a.erase(it); ++res; } for(auto asd : backup) a.insert(-asd); return res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int tmp; for(int i=0; i<n; ++i) cin >> tmp, a.insert(-tmp); int q, x, y; cin >> q; while(q--) { int t; cin >> t; if(t == 1) { cin >> x >> y; cout << solve(x,y) << '\n'; } else if(t == 2) { cin >> x; a.insert(-x); } else { cin >> x; auto it = a.find(-x); a.erase(it); } } return 0; } |