#include <iostream> using namespace std; struct Fish{ long long weight; bool active; Fish(){ active = true; } }; istream & operator >> (istream & is, Fish & f){ is >> f.weight; return is; } bool stuff(Fish * const fishes, const int & index, long long currentWeight, long long & expectedWeight, int depth, int & min){ if(currentWeight >= expectedWeight){ if(depth < min) min = depth; return true; } bool possible = false; for(int i = 0; i < index; ++i){ if(fishes[i].active && fishes[i].weight < currentWeight){ fishes[i].active = false; if(stuff(fishes, index, currentWeight + fishes[i].weight, expectedWeight, depth + 1, min)){ possible = true; } fishes[i].active = true; } } return possible; } int main(){ int n; cin >> n; int index = n; long long sum = 0; Fish * fishes = new Fish [2 * n]; for(int i = 0; i < n; ++i){ cin >> fishes[i]; sum += fishes[i].weight; } n = 2 * n; int q; long long a, b; cin >> q; for(int i = 0; i < q; ++i){ cin >> a; if(a == 1){ cin >> a >> b; if(b - a > sum){ cout << -1 << endl; continue; } int min = index; if(stuff(fishes, index, a, b, 0, min)){ cout << min << endl; }else{ cout << -1 << endl; } }else if(a == 2){ if(index >= n){ Fish * f2 = fishes; n *= 2; fishes = new Fish [n]; for(int j = 0; j < index; ++j){ fishes[j] = f2[j]; } delete [] f2; } cin >> fishes[index++]; sum += fishes[index - 1].weight; }else{ cin >> b; for(int j = 0; j < index; ++j){ if(fishes[j].weight == b && fishes[j].active){ fishes[j].active = false; sum -= fishes[j].weight; 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 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 | #include <iostream> using namespace std; struct Fish{ long long weight; bool active; Fish(){ active = true; } }; istream & operator >> (istream & is, Fish & f){ is >> f.weight; return is; } bool stuff(Fish * const fishes, const int & index, long long currentWeight, long long & expectedWeight, int depth, int & min){ if(currentWeight >= expectedWeight){ if(depth < min) min = depth; return true; } bool possible = false; for(int i = 0; i < index; ++i){ if(fishes[i].active && fishes[i].weight < currentWeight){ fishes[i].active = false; if(stuff(fishes, index, currentWeight + fishes[i].weight, expectedWeight, depth + 1, min)){ possible = true; } fishes[i].active = true; } } return possible; } int main(){ int n; cin >> n; int index = n; long long sum = 0; Fish * fishes = new Fish [2 * n]; for(int i = 0; i < n; ++i){ cin >> fishes[i]; sum += fishes[i].weight; } n = 2 * n; int q; long long a, b; cin >> q; for(int i = 0; i < q; ++i){ cin >> a; if(a == 1){ cin >> a >> b; if(b - a > sum){ cout << -1 << endl; continue; } int min = index; if(stuff(fishes, index, a, b, 0, min)){ cout << min << endl; }else{ cout << -1 << endl; } }else if(a == 2){ if(index >= n){ Fish * f2 = fishes; n *= 2; fishes = new Fish [n]; for(int j = 0; j < index; ++j){ fishes[j] = f2[j]; } delete [] f2; } cin >> fishes[index++]; sum += fishes[index - 1].weight; }else{ cin >> b; for(int j = 0; j < index; ++j){ if(fishes[j].weight == b && fishes[j].active){ fishes[j].active = false; sum -= fishes[j].weight; break; } } } } return 0; } |