#include <cstdio> #include <vector> #include <set> using namespace std; long long int n, a, b, c, t; multiset<long long int> os; int main(){ scanf("%lld", &n); for (int i = 0; i < n; ++i){ scanf("%lld", &a); os.insert(a); } scanf("%lld", &t); for (int i = 0; i < t; ++i){ scanf("%lld", &a); if(a == 1){ scanf("%lld %lld", &b, &c); long long int currentWeight = b; long long int eaten = 0; auto s = multiset<long long int>(os); // for (auto k = s.begin(); k != s.end(); ++k) { // cout << *k << " "; // } // cout << endl; if (s.empty()){ if (b >= c){ printf("0\n"); } else { printf("-1\n"); } continue; } auto it = s.end(); it--; if(currentWeight >= c) { printf("0\n"); continue; } if(*s.begin() >= currentWeight){ printf("-1\n"); continue; } else { // cout << "Looking for smaller than " << currentWeight << ": " << *it << endl; while(*it >= currentWeight){ // cout << *it << endl; it--; } // cout << "smaller!" << endl; } while(currentWeight < c){ if(s.empty()){ break; } if(it == s.end()){ it--; } // cout << "Looking at " << *it << endl; if(*it < currentWeight){ auto next = it; ++next; if(next != s.end() && *next < currentWeight){ ++it; } else { // cout << "Eating " << *it << endl; eaten ++; currentWeight += *it; s.erase(it++); } } else { if(it != s.begin()){ auto previous = it; previous--; // cout << "Eating " << *previous << endl; eaten++; currentWeight += *previous; s.erase(previous); } else { break; } } } // cout << currentWeight << endl; if(currentWeight >= c){ printf("%lld\n", eaten); } else { printf("-1\n"); } } else { scanf("%lld", &b); if (a == 2) { os.insert(b); } if (a == 3) { os.erase(b); } } } }
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 | #include <cstdio> #include <vector> #include <set> using namespace std; long long int n, a, b, c, t; multiset<long long int> os; int main(){ scanf("%lld", &n); for (int i = 0; i < n; ++i){ scanf("%lld", &a); os.insert(a); } scanf("%lld", &t); for (int i = 0; i < t; ++i){ scanf("%lld", &a); if(a == 1){ scanf("%lld %lld", &b, &c); long long int currentWeight = b; long long int eaten = 0; auto s = multiset<long long int>(os); // for (auto k = s.begin(); k != s.end(); ++k) { // cout << *k << " "; // } // cout << endl; if (s.empty()){ if (b >= c){ printf("0\n"); } else { printf("-1\n"); } continue; } auto it = s.end(); it--; if(currentWeight >= c) { printf("0\n"); continue; } if(*s.begin() >= currentWeight){ printf("-1\n"); continue; } else { // cout << "Looking for smaller than " << currentWeight << ": " << *it << endl; while(*it >= currentWeight){ // cout << *it << endl; it--; } // cout << "smaller!" << endl; } while(currentWeight < c){ if(s.empty()){ break; } if(it == s.end()){ it--; } // cout << "Looking at " << *it << endl; if(*it < currentWeight){ auto next = it; ++next; if(next != s.end() && *next < currentWeight){ ++it; } else { // cout << "Eating " << *it << endl; eaten ++; currentWeight += *it; s.erase(it++); } } else { if(it != s.begin()){ auto previous = it; previous--; // cout << "Eating " << *previous << endl; eaten++; currentWeight += *previous; s.erase(previous); } else { break; } } } // cout << currentWeight << endl; if(currentWeight >= c){ printf("%lld\n", eaten); } else { printf("-1\n"); } } else { scanf("%lld", &b); if (a == 2) { os.insert(b); } if (a == 3) { os.erase(b); } } } } |