#include <iostream> #include <algorithm> #include <vector> #include <list> #include <string> using namespace std; int main() { ios::sync_with_stdio(false); int n,typ,eaten; long long int tmp,k,SW=0; vector <long long int> loved1; loved1.reserve(400000); cin >> n; for (; n>0; n--){ cin >> tmp; SW+=tmp; loved1.push_back(tmp); } sort(loved1.begin(),loved1.end()); cin >> n; for (; n>0; n--){ cin >> typ; if(typ==1){ cin >> tmp >> k; if (k-SW>tmp) cout << "-1 \n"; else if (tmp==k) cout << "0 \n"; else{ list <int> vs; for (int i=0; loved1.size()>i; i++) vs.push_back(i); list<int>::iterator iter=vs.end(); eaten=0; bool f=0; while (!vs.empty()){ if (k>tmp){ iter=--vs.end(); while (loved1[*iter]>=tmp&&vs.size()!=1){ iter=prev(iter); if (iter==vs.begin()&&loved1[*iter]>=tmp){ f=1; break; } } if (iter==vs.begin()&&loved1[*iter]>=tmp){ f=1; break; } tmp+=loved1[*iter]; eaten++; vs.erase(iter); } else break; if (f) break; } if (!f) cout << eaten << "\n"; else cout << "-1 \n"; } } else if (typ==2){ cin >> tmp; SW+=tmp; loved1.insert(lower_bound(loved1.begin(), loved1.end(), tmp), tmp); } else{ cin >> tmp; SW+=tmp; loved1.erase(lower_bound(loved1.begin(), loved1.end(), tmp)); } } 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 | #include <iostream> #include <algorithm> #include <vector> #include <list> #include <string> using namespace std; int main() { ios::sync_with_stdio(false); int n,typ,eaten; long long int tmp,k,SW=0; vector <long long int> loved1; loved1.reserve(400000); cin >> n; for (; n>0; n--){ cin >> tmp; SW+=tmp; loved1.push_back(tmp); } sort(loved1.begin(),loved1.end()); cin >> n; for (; n>0; n--){ cin >> typ; if(typ==1){ cin >> tmp >> k; if (k-SW>tmp) cout << "-1 \n"; else if (tmp==k) cout << "0 \n"; else{ list <int> vs; for (int i=0; loved1.size()>i; i++) vs.push_back(i); list<int>::iterator iter=vs.end(); eaten=0; bool f=0; while (!vs.empty()){ if (k>tmp){ iter=--vs.end(); while (loved1[*iter]>=tmp&&vs.size()!=1){ iter=prev(iter); if (iter==vs.begin()&&loved1[*iter]>=tmp){ f=1; break; } } if (iter==vs.begin()&&loved1[*iter]>=tmp){ f=1; break; } tmp+=loved1[*iter]; eaten++; vs.erase(iter); } else break; if (f) break; } if (!f) cout << eaten << "\n"; else cout << "-1 \n"; } } else if (typ==2){ cin >> tmp; SW+=tmp; loved1.insert(lower_bound(loved1.begin(), loved1.end(), tmp), tmp); } else{ cin >> tmp; SW+=tmp; loved1.erase(lower_bound(loved1.begin(), loved1.end(), tmp)); } } return 0; } |