#include<cstdio> #include<map> struct cmp { bool operator() (const long long & a, const long long & b) const { return a > b; } }; typedef long long ll; typedef std::map<ll, int> mli; typedef std::map<ll, int, cmp> mlic; int n; mlic m; void input() { scanf("%d", &n); ll t; for (int i = 0; i < n; i++) { scanf("%lld", &t); m[t]++; } } void print(mlic & m) { for (mli::iterator it = m.begin(); it != m.end(); it++) { printf("%lld:%d\n", it -> first, it -> second); } } void add_back(mli & used) { for (mli::iterator it = used.begin(); it != used.end(); it++) { m[it -> first] += it -> second; } } void remove(ll w) { m[w]--; if (m[w] == 0) { m.erase(w); } } int queryAttack(ll s, ll k) { // printf("query %lld %lld\n", s, k); mli used; int i = 0; while (s < k) { mlic::iterator it = m.upper_bound(s); // printf("%lld %d\n", it -> first, it -> second); if (it == m.end()) { add_back(used); return -1; } ll key = it -> first; s += key; remove(key); used[key] += 1; i++; } add_back(used); return i; } void add(ll w) { m[w]++; } void query() { int type; scanf("%d", &type); ll a, b; if (type == 1) { scanf("%lld %lld", &a, &b); printf("%d\n", queryAttack(a, b)); } if (type == 2) { scanf("%lld", &a); add(a); } if (type == 3) { scanf("%lld", &a); remove(a); } // printf("---\n"); // print(m); } void process() { int q; scanf("%d", &q); while (q--) { query(); } } int main() { input(); // print(m); process(); 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 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 | #include<cstdio> #include<map> struct cmp { bool operator() (const long long & a, const long long & b) const { return a > b; } }; typedef long long ll; typedef std::map<ll, int> mli; typedef std::map<ll, int, cmp> mlic; int n; mlic m; void input() { scanf("%d", &n); ll t; for (int i = 0; i < n; i++) { scanf("%lld", &t); m[t]++; } } void print(mlic & m) { for (mli::iterator it = m.begin(); it != m.end(); it++) { printf("%lld:%d\n", it -> first, it -> second); } } void add_back(mli & used) { for (mli::iterator it = used.begin(); it != used.end(); it++) { m[it -> first] += it -> second; } } void remove(ll w) { m[w]--; if (m[w] == 0) { m.erase(w); } } int queryAttack(ll s, ll k) { // printf("query %lld %lld\n", s, k); mli used; int i = 0; while (s < k) { mlic::iterator it = m.upper_bound(s); // printf("%lld %d\n", it -> first, it -> second); if (it == m.end()) { add_back(used); return -1; } ll key = it -> first; s += key; remove(key); used[key] += 1; i++; } add_back(used); return i; } void add(ll w) { m[w]++; } void query() { int type; scanf("%d", &type); ll a, b; if (type == 1) { scanf("%lld %lld", &a, &b); printf("%d\n", queryAttack(a, b)); } if (type == 2) { scanf("%lld", &a); add(a); } if (type == 3) { scanf("%lld", &a); remove(a); } // printf("---\n"); // print(m); } void process() { int q; scanf("%d", &q); while (q--) { query(); } } int main() { input(); // print(m); process(); return 0; } |