#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; const short NO = 0; const short YES = 1; int binSearch(const vector<unsigned long long>& tab, int l, int r, unsigned long long wanted) { int mid; while (l <= r) { mid = (l + r) / 2; if (tab[mid] == wanted) return mid; if (tab[mid] > wanted) r = mid - 1; else l = mid + 1; } return -1; } int findFish(const vector<short>& eaten, const vector<unsigned long long>& fish, int l, int r, unsigned long long wanted) { int mid; int place = -1; while (l <= r) { mid = (l + r) / 2; if (fish[mid] == wanted) { place = mid; break; } if (l == r) { if (fish[mid] > wanted || mid == fish.size() - 1) place = mid; else place = mid + 1; break; } else { if (abs(l - r) == 1) r--; else { if (fish[mid] > wanted) r = mid - 1; else l = mid + 1; } } } if (place == fish.size() - 1 && eaten[place] == NO && fish[place] < wanted) return place; while (true) { place--; if (eaten[place] == NO) return place; } return -1; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector< unsigned long long> szprotki; szprotki.reserve(n); for (int i = 0; i < n; ++i) { unsigned long long t; cin >> t; szprotki.push_back(t); } sort(szprotki.begin(), szprotki.end()); int situations; cin >> situations; for (int i = 0; i < situations; ++i) { short type; cin >> type; if (type == 1) { unsigned long long weight, wanted, current; cin >> weight >> wanted; current = weight; int counter = 0; vector<short> eaten(szprotki.size()); fill(eaten.begin(), eaten.end(), NO); while (true) { if (current >= wanted) break; unsigned long long l = -1; for (int i = 0; i < eaten.size(); ++i) if (eaten[i] == NO) { l = szprotki[i]; break; } if (l == -1 || l >= current) { counter = -1; break; } int which = findFish(eaten, szprotki, 0, szprotki.size() - 1, current); eaten[which] = YES; current += szprotki[which]; ++counter; } cout << counter << endl; } if (type == 2) { long long s; cin >> s; szprotki.push_back(s); sort(szprotki.begin(), szprotki.end()); } if (type == 3) { long long s; cin >> s; szprotki.erase(szprotki.begin() + binSearch(szprotki, 0, szprotki.size() - 1, s)); } } 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; const short NO = 0; const short YES = 1; int binSearch(const vector<unsigned long long>& tab, int l, int r, unsigned long long wanted) { int mid; while (l <= r) { mid = (l + r) / 2; if (tab[mid] == wanted) return mid; if (tab[mid] > wanted) r = mid - 1; else l = mid + 1; } return -1; } int findFish(const vector<short>& eaten, const vector<unsigned long long>& fish, int l, int r, unsigned long long wanted) { int mid; int place = -1; while (l <= r) { mid = (l + r) / 2; if (fish[mid] == wanted) { place = mid; break; } if (l == r) { if (fish[mid] > wanted || mid == fish.size() - 1) place = mid; else place = mid + 1; break; } else { if (abs(l - r) == 1) r--; else { if (fish[mid] > wanted) r = mid - 1; else l = mid + 1; } } } if (place == fish.size() - 1 && eaten[place] == NO && fish[place] < wanted) return place; while (true) { place--; if (eaten[place] == NO) return place; } return -1; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector< unsigned long long> szprotki; szprotki.reserve(n); for (int i = 0; i < n; ++i) { unsigned long long t; cin >> t; szprotki.push_back(t); } sort(szprotki.begin(), szprotki.end()); int situations; cin >> situations; for (int i = 0; i < situations; ++i) { short type; cin >> type; if (type == 1) { unsigned long long weight, wanted, current; cin >> weight >> wanted; current = weight; int counter = 0; vector<short> eaten(szprotki.size()); fill(eaten.begin(), eaten.end(), NO); while (true) { if (current >= wanted) break; unsigned long long l = -1; for (int i = 0; i < eaten.size(); ++i) if (eaten[i] == NO) { l = szprotki[i]; break; } if (l == -1 || l >= current) { counter = -1; break; } int which = findFish(eaten, szprotki, 0, szprotki.size() - 1, current); eaten[which] = YES; current += szprotki[which]; ++counter; } cout << counter << endl; } if (type == 2) { long long s; cin >> s; szprotki.push_back(s); sort(szprotki.begin(), szprotki.end()); } if (type == 3) { long long s; cin >> s; szprotki.erase(szprotki.begin() + binSearch(szprotki, 0, szprotki.size() - 1, s)); } } return 0; } |