#include<bits/stdc++.h> using namespace std; #define ll long long int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int n,q; cin >> n; vector<ll> tab(n); for(int i=0;i<n;i++) { cin >> tab[i]; } sort(tab.begin(),tab.end()); cin >> q; while(q--) { assert(is_sorted(tab.begin(),tab.end())); ll t, s,k; cin >> t >> s; if(t == 1) { cin >> k; int ct = 0; stack<ll> sck; for(int i=0;i<n;i++) { if(s >= k) break; while(tab[i] >= s && sck.size() > 0) { s += sck.top(); sck.pop(); ct++; if(s >= k) break; } if(s >= k) break; if(tab[i] >= s) { ct = -1; break; } else sck.push(tab[i]); } while(sck.size() > 0 && s < k) { s += sck.top(); sck.pop(); ct++; } if(ct == -1 || s < k) cout<<-1<<"\n"; else cout<<ct<<"\n"; } else if(t == 2) { if(tab.size() == 0 || s < *tab.begin()) tab.insert(tab.begin(),s); else { for(int i=tab.size()-1;i>=0;i--) { if(tab[i] <= s){ tab.insert(tab.begin()+i+1,s); break; } } } } else { for(int i=tab.size()-1;i>=0;i--) { if(tab[i] == s){ tab.erase(tab.begin()+i); break; } } } } }
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 | #include<bits/stdc++.h> using namespace std; #define ll long long int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int n,q; cin >> n; vector<ll> tab(n); for(int i=0;i<n;i++) { cin >> tab[i]; } sort(tab.begin(),tab.end()); cin >> q; while(q--) { assert(is_sorted(tab.begin(),tab.end())); ll t, s,k; cin >> t >> s; if(t == 1) { cin >> k; int ct = 0; stack<ll> sck; for(int i=0;i<n;i++) { if(s >= k) break; while(tab[i] >= s && sck.size() > 0) { s += sck.top(); sck.pop(); ct++; if(s >= k) break; } if(s >= k) break; if(tab[i] >= s) { ct = -1; break; } else sck.push(tab[i]); } while(sck.size() > 0 && s < k) { s += sck.top(); sck.pop(); ct++; } if(ct == -1 || s < k) cout<<-1<<"\n"; else cout<<ct<<"\n"; } else if(t == 2) { if(tab.size() == 0 || s < *tab.begin()) tab.insert(tab.begin(),s); else { for(int i=tab.size()-1;i>=0;i--) { if(tab[i] <= s){ tab.insert(tab.begin()+i+1,s); break; } } } } else { for(int i=tab.size()-1;i>=0;i--) { if(tab[i] == s){ tab.erase(tab.begin()+i); break; } } } } } |