#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
map<LL,int> M;
vector<LL> V;
int asdf(LL s, LL k) {
V.clear();
int c = 0;
map<LL,int>::iterator it;
while (-s > -k) {
if (M.empty()) {
c = -1;
break;
}
it = M.upper_bound(-s);
if (it != M.end()) {
s += -(it->first);
V.push_back(it->first);
if (it->second == 1) {
M.erase(it);
} else {
it->second--;
}
c++;
} else {
c = -1;
break;
}
}
for (int i=0;i<V.size();i++) {
M[V[i]]++;
}
return c;
}
int main()
{
int n;
scanf("%d",&n);
LL a;
for (int i=0;i<n;i++) {
scanf("%lld",&a);
M[-a]++;
}
int q;
scanf("%d",&q);
int r;
LL s,k,w;
for (int i=0;i<q;i++) {
scanf("%d",&r);
if (r == 1) {
scanf("%lld%lld",&s,&k);
printf("%d\n",asdf(s,k));
} else {
scanf("%lld",&w);
if (r == 2) {
M[-w]++;
} else {
map<LL,int>::iterator it = M.find(-w);
if (it->second == 1) {
M.erase(it);
} else {
it->second--;
}
}
}
}
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 | #include <iostream> #include <vector> #include <cstdio> #include <algorithm> #include <map> #include <set> using namespace std; typedef long long LL; map<LL,int> M; vector<LL> V; int asdf(LL s, LL k) { V.clear(); int c = 0; map<LL,int>::iterator it; while (-s > -k) { if (M.empty()) { c = -1; break; } it = M.upper_bound(-s); if (it != M.end()) { s += -(it->first); V.push_back(it->first); if (it->second == 1) { M.erase(it); } else { it->second--; } c++; } else { c = -1; break; } } for (int i=0;i<V.size();i++) { M[V[i]]++; } return c; } int main() { int n; scanf("%d",&n); LL a; for (int i=0;i<n;i++) { scanf("%lld",&a); M[-a]++; } int q; scanf("%d",&q); int r; LL s,k,w; for (int i=0;i<q;i++) { scanf("%d",&r); if (r == 1) { scanf("%lld%lld",&s,&k); printf("%d\n",asdf(s,k)); } else { scanf("%lld",&w); if (r == 2) { M[-w]++; } else { map<LL,int>::iterator it = M.find(-w); if (it->second == 1) { M.erase(it); } else { it->second--; } } } } return 0; } |
English