#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
void dodaj(vector<long int> &szprot, long int waga) {
szprot.insert(lower_bound(szprot.begin(), szprot.end(), waga), waga);
}
void usun(vector<long int> &szprot, long int waga) {
szprot.erase(lower_bound(szprot.begin(), szprot.end(), waga));
}
int atak(vector<long int> szpr, long int waga1, long int waga2, int ryby) {
if (waga1 >= waga2) return ryby;
int indeks = lower_bound(szpr.begin(), szpr.end(), waga1) - szpr.begin();
if (indeks == 0) return -1;
else {
indeks--;
long int wagau = szpr.at(indeks);
szpr.erase(indeks + szpr.begin());
return atak(szpr, waga1 + wagau, waga2, ryby + 1);
}
}
int main() {
int n;
scanf("%d", &n);
vector<long int> szprot;
long int w;
for (int i = 0; i < n; ++i) {
scanf("%ld", &w);
szprot.emplace_back(w);
}
sort(szprot.begin(), szprot.end());
int wydarzenia;
scanf("%d", &wydarzenia);
int wydarz;
long int wa;
long int k;
for (int j = 0; j < wydarzenia; ++j) {
scanf("%d", &wydarz);
if (wydarz == 1) {
scanf("%ld %ld", &wa, &k);
printf("%d\n", atak(szprot, wa, k, 0));
} else if (wydarz == 2) {
scanf("%ld", &wa);
dodaj(szprot, wa);
} else {
scanf("%ld", &wa);
usun(szprot, wa);
}
}
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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; void dodaj(vector<long int> &szprot, long int waga) { szprot.insert(lower_bound(szprot.begin(), szprot.end(), waga), waga); } void usun(vector<long int> &szprot, long int waga) { szprot.erase(lower_bound(szprot.begin(), szprot.end(), waga)); } int atak(vector<long int> szpr, long int waga1, long int waga2, int ryby) { if (waga1 >= waga2) return ryby; int indeks = lower_bound(szpr.begin(), szpr.end(), waga1) - szpr.begin(); if (indeks == 0) return -1; else { indeks--; long int wagau = szpr.at(indeks); szpr.erase(indeks + szpr.begin()); return atak(szpr, waga1 + wagau, waga2, ryby + 1); } } int main() { int n; scanf("%d", &n); vector<long int> szprot; long int w; for (int i = 0; i < n; ++i) { scanf("%ld", &w); szprot.emplace_back(w); } sort(szprot.begin(), szprot.end()); int wydarzenia; scanf("%d", &wydarzenia); int wydarz; long int wa; long int k; for (int j = 0; j < wydarzenia; ++j) { scanf("%d", &wydarz); if (wydarz == 1) { scanf("%ld %ld", &wa, &k); printf("%d\n", atak(szprot, wa, k, 0)); } else if (wydarz == 2) { scanf("%ld", &wa); dodaj(szprot, wa); } else { scanf("%ld", &wa); usun(szprot, wa); } } return 0; } |
English