#include <iostream> #include <string> #include <vector> using namespace std; int count_range(const vector<int>& sums, int start, int end) { return sums[end] - sums[start]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k, t; cin >> n >> k >> t; string plan; cin >> plan; vector<int> office(n + 1, 0), home(n + 1, 0), vacant(n + 1, 0); for (int i = 1; i <= n; i++) { if (plan[i - 1] == '1') office[i] = office[i - 1] + 1; else office[i] = office[i - 1]; if (plan[i - 1] == '2') home[i] = home[i - 1] + 1; else home[i] = home[i - 1]; if (plan[i - 1] == '3') vacant[i] = vacant[i - 1] + 1; else vacant[i] = vacant[i - 1]; } int best = -1; if (office[n] <= k) { best = min(vacant[n] + k, n); } for (int to_office = 0; to_office <= n - 2 * t; ++to_office) for (int back_home = to_office + t; back_home <= n - t; ++back_home) { int skipped = 2 * t - count_range(vacant, to_office, to_office + t) - count_range(vacant, back_home, back_home + t); int skips_left = k - skipped - count_range(office, 0, to_office) - count_range(office, back_home + t, n); if (skips_left < 0) continue; int option = count_range(vacant, 0, to_office) + count_range(vacant, back_home + t, n) + count_range(office, 0, to_office) + count_range(office, back_home + t, n) + min(skips_left, count_range(home, 0, to_office) + count_range(home, back_home + t, n)); if (option > best) best = option; } cout << best << endl; }
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 | #include <iostream> #include <string> #include <vector> using namespace std; int count_range(const vector<int>& sums, int start, int end) { return sums[end] - sums[start]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k, t; cin >> n >> k >> t; string plan; cin >> plan; vector<int> office(n + 1, 0), home(n + 1, 0), vacant(n + 1, 0); for (int i = 1; i <= n; i++) { if (plan[i - 1] == '1') office[i] = office[i - 1] + 1; else office[i] = office[i - 1]; if (plan[i - 1] == '2') home[i] = home[i - 1] + 1; else home[i] = home[i - 1]; if (plan[i - 1] == '3') vacant[i] = vacant[i - 1] + 1; else vacant[i] = vacant[i - 1]; } int best = -1; if (office[n] <= k) { best = min(vacant[n] + k, n); } for (int to_office = 0; to_office <= n - 2 * t; ++to_office) for (int back_home = to_office + t; back_home <= n - t; ++back_home) { int skipped = 2 * t - count_range(vacant, to_office, to_office + t) - count_range(vacant, back_home, back_home + t); int skips_left = k - skipped - count_range(office, 0, to_office) - count_range(office, back_home + t, n); if (skips_left < 0) continue; int option = count_range(vacant, 0, to_office) + count_range(vacant, back_home + t, n) + count_range(office, 0, to_office) + count_range(office, back_home + t, n) + min(skips_left, count_range(home, 0, to_office) + count_range(home, back_home + t, n)); if (option > best) best = option; } cout << best << endl; } |