#include <iostream> #include <vector> using namespace std; int n, k, t; string day; enum TRIP { B_HOME, A_HOME, OFFFICE }; struct state { int points; int abandoned; int optional; TRIP trip = B_HOME; }; int life(state* curr, int idx) { if (curr->abandoned > k) { return -1; } //koniec if (idx >= n - 1) { if (idx == (n - 1))//normalne { if (day[idx] == '1') //biuro { curr->abandoned++; curr->points++; } else if (day[idx] == '2') //zdalne { curr->optional++; } else//nic { curr->points++; } if (curr->abandoned > k) { return -1; } } int diff = k - curr->abandoned; return curr->points + min(diff, curr->optional); } //przed podroz if (curr->trip == B_HOME) { int v1 = -1; //start podrozy if (idx <= (n - (2 * t) - 1)) { int aban = 0; for (int i = idx; i < idx + t; i++) { if (day[i] == '1' || day[i] == '2') { aban++; } } //t wartosci v1 = life(new state{ curr->points, curr->abandoned + aban, curr->optional, OFFFICE }, idx + t); } //dalej if (day[idx] == '1') //biuro { curr->abandoned++; curr->points++; } else if (day[idx] == '2') //zdalne { curr->optional++; } else//nic { curr->points++; } return max(v1, life(curr, idx + 1)); } else if (curr->trip == OFFFICE)//biuro { int v1 = -1; //zostac ile moge if (idx < n - t) { v1 = life(new state{ curr->points, curr->abandoned, curr->optional, OFFFICE }, idx + 1); } //powrót //wartosci podrozy for (int i = idx; i < idx + t; i++) { if (day[i] == '1' || day[i] == '2') { curr->abandoned++; } } curr->trip = A_HOME; return max(v1, life(curr, (idx + t))); } else if (curr->trip == A_HOME)//po podrozy liniowo { if (day[idx] == '1') //biuro { curr->abandoned++; curr->points++; } else if (day[idx] == '2') //zdalne { curr->optional++; } else//nic { curr->points++; } return life(curr, idx + 1); } } int main() { cin >> n >> k >> t; cin >> day; cout << life(new state(), 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 142 143 144 145 146 | #include <iostream> #include <vector> using namespace std; int n, k, t; string day; enum TRIP { B_HOME, A_HOME, OFFFICE }; struct state { int points; int abandoned; int optional; TRIP trip = B_HOME; }; int life(state* curr, int idx) { if (curr->abandoned > k) { return -1; } //koniec if (idx >= n - 1) { if (idx == (n - 1))//normalne { if (day[idx] == '1') //biuro { curr->abandoned++; curr->points++; } else if (day[idx] == '2') //zdalne { curr->optional++; } else//nic { curr->points++; } if (curr->abandoned > k) { return -1; } } int diff = k - curr->abandoned; return curr->points + min(diff, curr->optional); } //przed podroz if (curr->trip == B_HOME) { int v1 = -1; //start podrozy if (idx <= (n - (2 * t) - 1)) { int aban = 0; for (int i = idx; i < idx + t; i++) { if (day[i] == '1' || day[i] == '2') { aban++; } } //t wartosci v1 = life(new state{ curr->points, curr->abandoned + aban, curr->optional, OFFFICE }, idx + t); } //dalej if (day[idx] == '1') //biuro { curr->abandoned++; curr->points++; } else if (day[idx] == '2') //zdalne { curr->optional++; } else//nic { curr->points++; } return max(v1, life(curr, idx + 1)); } else if (curr->trip == OFFFICE)//biuro { int v1 = -1; //zostac ile moge if (idx < n - t) { v1 = life(new state{ curr->points, curr->abandoned, curr->optional, OFFFICE }, idx + 1); } //powrót //wartosci podrozy for (int i = idx; i < idx + t; i++) { if (day[i] == '1' || day[i] == '2') { curr->abandoned++; } } curr->trip = A_HOME; return max(v1, life(curr, (idx + t))); } else if (curr->trip == A_HOME)//po podrozy liniowo { if (day[idx] == '1') //biuro { curr->abandoned++; curr->points++; } else if (day[idx] == '2') //zdalne { curr->optional++; } else//nic { curr->points++; } return life(curr, idx + 1); } } int main() { cin >> n >> k >> t; cin >> day; cout << life(new state(), 0); } |