#include <vector> #include <iostream> using namespace std; class Counter { string m_schedule; int m_n; std::vector<int> officeAfter, remoteAfter; public: explicit Counter(const int n, string schedule) : m_schedule(std::move(schedule)), m_n(n), officeAfter(n), remoteAfter(n) { officeAfter[n - 1] = m_schedule[n - 1] == '1' ? 1 : 0; remoteAfter[n - 1] = m_schedule[n - 1] == '2' ? 1 : 0; for (int i = n - 2; i >= 0; i--) { const auto office_count = m_schedule[i] == '1' ? 1 : 0; const auto remote_count = m_schedule[i] == '2' ? 1 : 0; officeAfter[i] = officeAfter[i + 1] + office_count; remoteAfter[i] = remoteAfter[i + 1] + remote_count; } } [[nodiscard]] int countOffice(const int from, const int to) const { if (from > to) { return 0; } if (from == to) { return m_schedule[from] == '1' ? 1 : 0; } const auto afterTo = officeAfter[to]; const auto afterFrom = officeAfter[from]; return afterFrom - afterTo + (m_schedule[to] == '1' ? 1 : 0); } [[nodiscard]] int countRemote(const int from, const int to) const { if (from > to) { return 0; } if (from == to) { return m_schedule[from] == '2' ? 1 : 0; } const auto afterTo = remoteAfter[to]; const auto afterFrom = remoteAfter[from]; return afterFrom - afterTo + (m_schedule[to] == '2' ? 1 : 0); } }; int main() { ios_base::sync_with_stdio(false); int n, k, t; cin >> n >> k >> t; string word; cin >> word; const Counter counter(n, word); const auto office_meetings_cnt = counter.countOffice(0, n - 1); const auto remote_meetings_cnt = counter.countRemote(0, n - 1); if (office_meetings_cnt <= k) { // can stay home const auto mandatory_meetings_cnt = max(0, office_meetings_cnt + remote_meetings_cnt - k); cout << (n - mandatory_meetings_cnt) << endl; } else { // must go work int result = -1; for (int start = 0; start <= n - 2 * t; start++) { for (int end = start + t; end <= n - t; end++) { const int office_meetings_when_going_work = counter.countOffice(start, start + t - 1); const int remote_meetings_when_going_work = counter.countRemote(start, start + t - 1); const int office_meetings_when_going_home = counter.countOffice(end, end + t - 1); const int remote_meetings_when_going_home = counter.countRemote(end, end + t - 1); const int meetings_when_commuting = office_meetings_when_going_work + remote_meetings_when_going_work + office_meetings_when_going_home + remote_meetings_when_going_home; if (meetings_when_commuting > k) { continue; } const int can_skip_yet = k - meetings_when_commuting; const int office_meetings_before_work = counter.countOffice(0, start - 1); const int office_meetings_after_work = counter.countOffice(end + t, n - 1); const int office_meetings_when_home = office_meetings_before_work + office_meetings_after_work; const int remote_meetings_before_work = counter.countRemote(0, start - 1); const int remote_meetings_after_work = counter.countRemote(end + t, n - 1); const int remote_meetings_when_home = remote_meetings_before_work + remote_meetings_after_work; if (office_meetings_when_home <= can_skip_yet) { const int mandatory_meetings_cnt = max( 0, office_meetings_when_home + remote_meetings_when_home - can_skip_yet); const int time_outside_home = end + t - start; int local_result = n - mandatory_meetings_cnt - time_outside_home; result = max(result, local_result); } } } cout << result << endl; } 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 | #include <vector> #include <iostream> using namespace std; class Counter { string m_schedule; int m_n; std::vector<int> officeAfter, remoteAfter; public: explicit Counter(const int n, string schedule) : m_schedule(std::move(schedule)), m_n(n), officeAfter(n), remoteAfter(n) { officeAfter[n - 1] = m_schedule[n - 1] == '1' ? 1 : 0; remoteAfter[n - 1] = m_schedule[n - 1] == '2' ? 1 : 0; for (int i = n - 2; i >= 0; i--) { const auto office_count = m_schedule[i] == '1' ? 1 : 0; const auto remote_count = m_schedule[i] == '2' ? 1 : 0; officeAfter[i] = officeAfter[i + 1] + office_count; remoteAfter[i] = remoteAfter[i + 1] + remote_count; } } [[nodiscard]] int countOffice(const int from, const int to) const { if (from > to) { return 0; } if (from == to) { return m_schedule[from] == '1' ? 1 : 0; } const auto afterTo = officeAfter[to]; const auto afterFrom = officeAfter[from]; return afterFrom - afterTo + (m_schedule[to] == '1' ? 1 : 0); } [[nodiscard]] int countRemote(const int from, const int to) const { if (from > to) { return 0; } if (from == to) { return m_schedule[from] == '2' ? 1 : 0; } const auto afterTo = remoteAfter[to]; const auto afterFrom = remoteAfter[from]; return afterFrom - afterTo + (m_schedule[to] == '2' ? 1 : 0); } }; int main() { ios_base::sync_with_stdio(false); int n, k, t; cin >> n >> k >> t; string word; cin >> word; const Counter counter(n, word); const auto office_meetings_cnt = counter.countOffice(0, n - 1); const auto remote_meetings_cnt = counter.countRemote(0, n - 1); if (office_meetings_cnt <= k) { // can stay home const auto mandatory_meetings_cnt = max(0, office_meetings_cnt + remote_meetings_cnt - k); cout << (n - mandatory_meetings_cnt) << endl; } else { // must go work int result = -1; for (int start = 0; start <= n - 2 * t; start++) { for (int end = start + t; end <= n - t; end++) { const int office_meetings_when_going_work = counter.countOffice(start, start + t - 1); const int remote_meetings_when_going_work = counter.countRemote(start, start + t - 1); const int office_meetings_when_going_home = counter.countOffice(end, end + t - 1); const int remote_meetings_when_going_home = counter.countRemote(end, end + t - 1); const int meetings_when_commuting = office_meetings_when_going_work + remote_meetings_when_going_work + office_meetings_when_going_home + remote_meetings_when_going_home; if (meetings_when_commuting > k) { continue; } const int can_skip_yet = k - meetings_when_commuting; const int office_meetings_before_work = counter.countOffice(0, start - 1); const int office_meetings_after_work = counter.countOffice(end + t, n - 1); const int office_meetings_when_home = office_meetings_before_work + office_meetings_after_work; const int remote_meetings_before_work = counter.countRemote(0, start - 1); const int remote_meetings_after_work = counter.countRemote(end + t, n - 1); const int remote_meetings_when_home = remote_meetings_before_work + remote_meetings_after_work; if (office_meetings_when_home <= can_skip_yet) { const int mandatory_meetings_cnt = max( 0, office_meetings_when_home + remote_meetings_when_home - can_skip_yet); const int time_outside_home = end + t - start; int local_result = n - mandatory_meetings_cnt - time_outside_home; result = max(result, local_result); } } } cout << result << endl; } return 0; } |