#include <cstdio> #include <vector> #include <map> using namespace std; #define ulli unsigned long long int #define FOR(i,a,b) for(int (i)=(a); (i)!=(b); ++(i)) // ---------------------------------------------------------- typedef map<ulli, int, std::greater<ulli> > FishMap; FishMap M; void erase(ulli w) { M[w] -= 1; if (M[w] == 0) M.erase(w); } void insert(ulli w) { M[w] += 1; } // ---------------------------------------------------------- int solve(ulli s, ulli k) { vector<ulli> deleted; int result = 0; while (s < k) { FishMap::iterator it = M.upper_bound(s); if (it == M.end()) { result = -1; break; } deleted.push_back(it->first); erase(it->first); s += it->first; ++result; } FOR(i,0,deleted.size()) insert(deleted[i]); return result; } // ---------------------------------------------------------- int main() { int n, q, cmd; ulli s, k, w; scanf("%d", &n); FOR(i,0,n) { scanf("%llu", &w); insert(w); } scanf("%d", &q); FOR(i,0,q) { scanf("%d", &cmd); if (cmd == 1) { scanf("%llu %llu", &s, &k); printf("%d\n", solve(s, k)); } else if (cmd == 2) { scanf("%llu", &w); insert(w); } else { scanf("%llu", &w); erase(w); } } }
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 | #include <cstdio> #include <vector> #include <map> using namespace std; #define ulli unsigned long long int #define FOR(i,a,b) for(int (i)=(a); (i)!=(b); ++(i)) // ---------------------------------------------------------- typedef map<ulli, int, std::greater<ulli> > FishMap; FishMap M; void erase(ulli w) { M[w] -= 1; if (M[w] == 0) M.erase(w); } void insert(ulli w) { M[w] += 1; } // ---------------------------------------------------------- int solve(ulli s, ulli k) { vector<ulli> deleted; int result = 0; while (s < k) { FishMap::iterator it = M.upper_bound(s); if (it == M.end()) { result = -1; break; } deleted.push_back(it->first); erase(it->first); s += it->first; ++result; } FOR(i,0,deleted.size()) insert(deleted[i]); return result; } // ---------------------------------------------------------- int main() { int n, q, cmd; ulli s, k, w; scanf("%d", &n); FOR(i,0,n) { scanf("%llu", &w); insert(w); } scanf("%d", &q); FOR(i,0,q) { scanf("%d", &cmd); if (cmd == 1) { scanf("%llu %llu", &s, &k); printf("%d\n", solve(s, k)); } else if (cmd == 2) { scanf("%llu", &w); insert(w); } else { scanf("%llu", &w); erase(w); } } } |