#include <iostream> #include<list> #include<iterator> using namespace std; int n; long long int * w; bool * z; int s = 0; void Szczupak() { bool* z = new bool[n] {false}; long long int s, k; cin >> s >> k; if (w[0] >= s) { cout << "-1" << endl; return; } int ileSzprotek = 0; int i = 0; while (s < k && ileSzprotek < n) { bool zjedzona = false; while (w[i + 1] < s && i + 1 < n) i++; while (i >= 0) { if (!z[i]) { z[i] = true; s += w[i]; zjedzona = true; ileSzprotek++; break; } i--; } if (!zjedzona){ cout << "-1" << endl; return; } } if (s < k) { cout << "-1" << endl; return; } else { cout << ileSzprotek << endl; return; } } void Dodaj() { long long int v; cin >> v; long long int * w1 = new long long int[n + 1 - s]; int i = 0; while (v >= w[i+s] && i+s<n) { w1[i] = w[i+s]; i++; } w1[i] = v; i++; while (i + s <= n) { w1[i] = w[i + s - 1]; i++; } n = n + 1 - s; s = 0; w = w1; } void Ujmij() { long long int v; cin >> v; int i = 0; while (w[i] != v) i++; while (i > 0) { w[i] = w[i - 1]; i--; } w[0] = 0; s++; } int main() { cin >> n; w = new long long int[n]; for (int i = 0; i < n; i++) { int v; cin >> v; int j = 0; for (; j < i; j++) { if (v < w[j]) { break; } } for (int k = i; k > j; k--) { w[k] = w[k - 1]; } w[j] = v; } int q; cin >> q; for (int i = 0; i < q; i++) { int typ; cin >> typ; if (typ == 1) Szczupak(); else if (typ == 2) Dodaj(); else if (typ == 3) Ujmij(); } }
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 | #include <iostream> #include<list> #include<iterator> using namespace std; int n; long long int * w; bool * z; int s = 0; void Szczupak() { bool* z = new bool[n] {false}; long long int s, k; cin >> s >> k; if (w[0] >= s) { cout << "-1" << endl; return; } int ileSzprotek = 0; int i = 0; while (s < k && ileSzprotek < n) { bool zjedzona = false; while (w[i + 1] < s && i + 1 < n) i++; while (i >= 0) { if (!z[i]) { z[i] = true; s += w[i]; zjedzona = true; ileSzprotek++; break; } i--; } if (!zjedzona){ cout << "-1" << endl; return; } } if (s < k) { cout << "-1" << endl; return; } else { cout << ileSzprotek << endl; return; } } void Dodaj() { long long int v; cin >> v; long long int * w1 = new long long int[n + 1 - s]; int i = 0; while (v >= w[i+s] && i+s<n) { w1[i] = w[i+s]; i++; } w1[i] = v; i++; while (i + s <= n) { w1[i] = w[i + s - 1]; i++; } n = n + 1 - s; s = 0; w = w1; } void Ujmij() { long long int v; cin >> v; int i = 0; while (w[i] != v) i++; while (i > 0) { w[i] = w[i - 1]; i--; } w[0] = 0; s++; } int main() { cin >> n; w = new long long int[n]; for (int i = 0; i < n; i++) { int v; cin >> v; int j = 0; for (; j < i; j++) { if (v < w[j]) { break; } } for (int k = i; k > j; k--) { w[k] = w[k - 1]; } w[j] = v; } int q; cin >> q; for (int i = 0; i < q; i++) { int typ; cin >> typ; if (typ == 1) Szczupak(); else if (typ == 2) Dodaj(); else if (typ == 3) Ujmij(); } } |