#pragma GCC optimize("03") #pragma GCC optimize("Ofast") #pragma GCC optimize("tree-fre") #pragma GCC optimize("loop-interchange") #pragma GCC optimize("tree-vectorize") #include <bits/stdc++.h> using namespace std; int n,q; multiset <long long> aq; long long simulate(long long s, long long k){ int steps = 0; vector <long long> eaten; while(s < k && aq.size() > 0){ auto it = aq.lower_bound(s); if(*it >= s && it == aq.begin()){ for(int a = 0; a < eaten.size(); a++){ aq.insert(eaten[a]); } return -1; } if(*it >= s || it == aq.end()) it--; long long max_fish = *it; aq.erase(it); eaten.push_back(max_fish); s += max_fish; steps++; } for(int a = 0; a < eaten.size(); a++){ aq.insert(eaten[a]); } if(s >= k) return steps; return -1; } void obt(){ for(int a = 0; a < n; a++){ long long s; cin >> s; aq.insert(s); } } void sol(){ cin >> q; for(int a = 0; a < q; a++){ int type; cin >> type; if(type == 1){ long long s,k; cin >> s >> k; cout << simulate(s,k) << "\n"; } if(type == 2){ long long m; cin >> m; aq.insert(m); } if(type == 3){ long long m; cin >> m; auto it = aq.find(m); aq.erase(it); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; obt(); sol(); }
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 | #pragma GCC optimize("03") #pragma GCC optimize("Ofast") #pragma GCC optimize("tree-fre") #pragma GCC optimize("loop-interchange") #pragma GCC optimize("tree-vectorize") #include <bits/stdc++.h> using namespace std; int n,q; multiset <long long> aq; long long simulate(long long s, long long k){ int steps = 0; vector <long long> eaten; while(s < k && aq.size() > 0){ auto it = aq.lower_bound(s); if(*it >= s && it == aq.begin()){ for(int a = 0; a < eaten.size(); a++){ aq.insert(eaten[a]); } return -1; } if(*it >= s || it == aq.end()) it--; long long max_fish = *it; aq.erase(it); eaten.push_back(max_fish); s += max_fish; steps++; } for(int a = 0; a < eaten.size(); a++){ aq.insert(eaten[a]); } if(s >= k) return steps; return -1; } void obt(){ for(int a = 0; a < n; a++){ long long s; cin >> s; aq.insert(s); } } void sol(){ cin >> q; for(int a = 0; a < q; a++){ int type; cin >> type; if(type == 1){ long long s,k; cin >> s >> k; cout << simulate(s,k) << "\n"; } if(type == 2){ long long m; cin >> m; aq.insert(m); } if(type == 3){ long long m; cin >> m; auto it = aq.find(m); aq.erase(it); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; obt(); sol(); } |