#include <bits/stdc++.h>
using namespace std;
int n, q;
vector<long long> fish;
void insert(const long long& weight)
{
for (vector<long long>::iterator i = fish.begin(); i != fish.end(); i++)
{
if (*i >= weight)
{
fish.insert(i, weight);
return;
}
}
fish.push_back(weight);
}
void erase(const long long& weight)
{
for (vector<long long>::iterator i = fish.begin(); i != fish.end(); i++)
{
if (*i == weight)
{
fish.erase(i);
return;
}
}
}
long long jump(long long& b, const long long& e)
{
long long odp = 0;
stack<long long> s;
for (long long& i : fish)
{
while (!s.empty() && b <= i && b < e)
{
++odp;
b += s.top();
s.pop();
}
if (b >= e)
return odp;
if (i < b)
s.push(i);
else
return -1;
}
while (!s.empty() && b < e)
{
++odp;
b += s.top();
s.pop();
}
return b >= e ? odp : -1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n; fish.resize(n);
for (long long& i : fish)
cin >> i;
sort(fish.begin(), fish.end());
cin >> q;
for (int i = 0; i < q; i++)
{
int type;
cin >> type;
if (type == 1)
{
long long begin, end;
cin >> begin >> end;
cout << jump(begin, end) << '\n';
}
else
{
long long weight;
cin >> weight;
type == 2 ? insert(weight) : erase(weight);
}
}
}
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 | #include <bits/stdc++.h> using namespace std; int n, q; vector<long long> fish; void insert(const long long& weight) { for (vector<long long>::iterator i = fish.begin(); i != fish.end(); i++) { if (*i >= weight) { fish.insert(i, weight); return; } } fish.push_back(weight); } void erase(const long long& weight) { for (vector<long long>::iterator i = fish.begin(); i != fish.end(); i++) { if (*i == weight) { fish.erase(i); return; } } } long long jump(long long& b, const long long& e) { long long odp = 0; stack<long long> s; for (long long& i : fish) { while (!s.empty() && b <= i && b < e) { ++odp; b += s.top(); s.pop(); } if (b >= e) return odp; if (i < b) s.push(i); else return -1; } while (!s.empty() && b < e) { ++odp; b += s.top(); s.pop(); } return b >= e ? odp : -1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; fish.resize(n); for (long long& i : fish) cin >> i; sort(fish.begin(), fish.end()); cin >> q; for (int i = 0; i < q; i++) { int type; cin >> type; if (type == 1) { long long begin, end; cin >> begin >> end; cout << jump(begin, end) << '\n'; } else { long long weight; cin >> weight; type == 2 ? insert(weight) : erase(weight); } } } |
English