#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int n, q, typ, it, zjedzone, jestCos;
long long s, k, w, suma = 0;
cin >> n;
vector<long long> szprotki(n);
vector<int> odwiedzone(310000);
vector<long long>::iterator znal;
for(int iN = 0; iN < n; ++iN) {
cin >> szprotki[iN];
suma += szprotki[iN];
}
sort(szprotki.begin(), szprotki.end(), greater<long long>());
cin >> q;
for(int iQ = 1; iQ <= q; ++iQ) {
cin >> typ;
switch(typ) {
case 1:
cin >> s >> k;
zjedzone = 0;
if((k - s) <= suma)
while(s < k) {
znal = lower_bound(szprotki.begin(), szprotki.end(), s - 1, greater<long long>());
if(znal == szprotki.end())
break;
jestCos = 0;
for(it = znal - szprotki.begin(); it < szprotki.end() - szprotki.begin(); ++it)
if(odwiedzone[it] != iQ) {
odwiedzone[it] = iQ;
s += szprotki[it];
++zjedzone;
jestCos = 1;
break;
}
if(!jestCos)
break;
}
cout << ((s < k) ? -1 : zjedzone) << endl;
break;
case 2:
cin >> w;
suma += w;
szprotki.push_back(w);
sort(szprotki.begin(), szprotki.end(), greater<long long>());
break;
case 3:
cin >> w;
suma -= w;
znal = lower_bound(szprotki.begin(), szprotki.end(), w, greater<long long>());
szprotki[znal - szprotki.begin()] = -1;
sort(szprotki.begin(), szprotki.end(), greater<long long>());
szprotki.resize(szprotki.size() - 1);
break;
}
}
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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(0); int n, q, typ, it, zjedzone, jestCos; long long s, k, w, suma = 0; cin >> n; vector<long long> szprotki(n); vector<int> odwiedzone(310000); vector<long long>::iterator znal; for(int iN = 0; iN < n; ++iN) { cin >> szprotki[iN]; suma += szprotki[iN]; } sort(szprotki.begin(), szprotki.end(), greater<long long>()); cin >> q; for(int iQ = 1; iQ <= q; ++iQ) { cin >> typ; switch(typ) { case 1: cin >> s >> k; zjedzone = 0; if((k - s) <= suma) while(s < k) { znal = lower_bound(szprotki.begin(), szprotki.end(), s - 1, greater<long long>()); if(znal == szprotki.end()) break; jestCos = 0; for(it = znal - szprotki.begin(); it < szprotki.end() - szprotki.begin(); ++it) if(odwiedzone[it] != iQ) { odwiedzone[it] = iQ; s += szprotki[it]; ++zjedzone; jestCos = 1; break; } if(!jestCos) break; } cout << ((s < k) ? -1 : zjedzone) << endl; break; case 2: cin >> w; suma += w; szprotki.push_back(w); sort(szprotki.begin(), szprotki.end(), greater<long long>()); break; case 3: cin >> w; suma -= w; znal = lower_bound(szprotki.begin(), szprotki.end(), w, greater<long long>()); szprotki[znal - szprotki.begin()] = -1; sort(szprotki.begin(), szprotki.end(), greater<long long>()); szprotki.resize(szprotki.size() - 1); break; } } return 0; } |
English