// Jakub Daszkiewicz // zadanie Szprotki i Szczupaki [B] // // test 1: 4 1 4 8 1 15 1 2 3 1 2 4 1 2 5 1 3 3 1 3 5 1 3 16 1 4 16 1 8 17 1 100 101 1 100 115 1 3 9 2 2 1 3 9 3 4 1 3 9 => 1 2 -1 0 2 4 3 2 1 -1 3 2 -1 #include <stdio.h> #include <vector> #include <algorithm> #define ull unsigned long long using namespace std; vector<ull> fish; void fish_erase_value(ull value) { ull begin = 0, end = fish.size()-1, check; while (true) { check = (begin+end)/2; if (fish[check] > value) end = check; else if (fish[check] < value) begin = check; else { fish.erase(fish.begin()+check); return; } } } ull fish_find_uneaten(ull value, bool e[]) { for (ull i=0; i<fish.size(); i++) { if (fish[i] == value && !e[i]) return i; } return 18446744073709551615; } ull fish_find_greater(ull value) { for (ull i=0; i<fish.size(); i++) { if (fish[i] >= value) return i; } return fish.size(); } bool less_true(bool b[], ull v) { for (int i=v-1; i>=0; i--) { if (b[i] == false) return false; } return true; } int main() { ull fish_amount; scanf("%llu", &fish_amount); ull t; for (ull i=0; i<fish_amount; i++) { scanf("%llu", &t); fish.push_back(t); } sort(fish.begin(), fish.end()); ull q, p_w, p_g; long long r; scanf("%llu", &q); unsigned int type; for (ull p=0; p<q; p++) { scanf("%u", &type); switch (type) { case 1: { r = 0; scanf("%llu%llu", &p_w, &p_g); bool eaten[fish.size()] = {}; while (true) { if (p_w >= p_g) break; for (ull i=p_w-1; i>0; i--) { if (fish_find_uneaten(i, eaten) == 18446744073709551615) continue; else { p_w += i; eaten[fish_find_uneaten(i, eaten)] = true; r++; break; } } if (p_w < p_g && less_true(eaten, fish_find_greater(p_w))) { r = -1; break; } } printf("%lld\n", r); break; } case 2: { scanf("%llu", &t); fish.push_back(t); sort(fish.begin(), fish.end()); break; } case 3: { scanf("%llu", &t); fish_erase_value(t); 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | // Jakub Daszkiewicz // zadanie Szprotki i Szczupaki [B] // // test 1: 4 1 4 8 1 15 1 2 3 1 2 4 1 2 5 1 3 3 1 3 5 1 3 16 1 4 16 1 8 17 1 100 101 1 100 115 1 3 9 2 2 1 3 9 3 4 1 3 9 => 1 2 -1 0 2 4 3 2 1 -1 3 2 -1 #include <stdio.h> #include <vector> #include <algorithm> #define ull unsigned long long using namespace std; vector<ull> fish; void fish_erase_value(ull value) { ull begin = 0, end = fish.size()-1, check; while (true) { check = (begin+end)/2; if (fish[check] > value) end = check; else if (fish[check] < value) begin = check; else { fish.erase(fish.begin()+check); return; } } } ull fish_find_uneaten(ull value, bool e[]) { for (ull i=0; i<fish.size(); i++) { if (fish[i] == value && !e[i]) return i; } return 18446744073709551615; } ull fish_find_greater(ull value) { for (ull i=0; i<fish.size(); i++) { if (fish[i] >= value) return i; } return fish.size(); } bool less_true(bool b[], ull v) { for (int i=v-1; i>=0; i--) { if (b[i] == false) return false; } return true; } int main() { ull fish_amount; scanf("%llu", &fish_amount); ull t; for (ull i=0; i<fish_amount; i++) { scanf("%llu", &t); fish.push_back(t); } sort(fish.begin(), fish.end()); ull q, p_w, p_g; long long r; scanf("%llu", &q); unsigned int type; for (ull p=0; p<q; p++) { scanf("%u", &type); switch (type) { case 1: { r = 0; scanf("%llu%llu", &p_w, &p_g); bool eaten[fish.size()] = {}; while (true) { if (p_w >= p_g) break; for (ull i=p_w-1; i>0; i--) { if (fish_find_uneaten(i, eaten) == 18446744073709551615) continue; else { p_w += i; eaten[fish_find_uneaten(i, eaten)] = true; r++; break; } } if (p_w < p_g && less_true(eaten, fish_find_greater(p_w))) { r = -1; break; } } printf("%lld\n", r); break; } case 2: { scanf("%llu", &t); fish.push_back(t); sort(fish.begin(), fish.end()); break; } case 3: { scanf("%llu", &t); fish_erase_value(t); break; } } } return 0; } |