#include <iostream>
using namespace std;
int count_meetings(string &s, int l, int r, bool office) {
int count = 0;
for (int i = l; i < r + 1; i++) {
if (s[i] == '1') {
count++;
} else if (s[i] == '2' and !office) {
count++;
}
}
return count;
}
int count_missed(string &s, int t, int l, int r) {
int count = 0;
for (int i = 0; i < t; i++) {
if (s[i + l] == '1' or s[i + l] == '2') count++;
if (s[r - i] == '1' or s[r - i] == '2') count++;
}
return count;
}
int main() {
int n, k, t, r, l = 0, max_algs = -1, algs;
string s;
cin >> n >> k >> t >> s;
int office_meetings = count_meetings(s, 0, n - 1, true);
if (office_meetings <= k) {
k -= office_meetings;
int home_meetings = 0;
for (char c : s) {
if (c == '2') home_meetings++;
}
algs = office_meetings;
for (char c : s) {
if (c == '3') algs++;
}
if (home_meetings <= k) {
cout << home_meetings + algs;
return 0;
} else {
cout << algs;
return 0;
}
}
while (n + 1 - t >= l + t) {
r = n - 1;
while (r - t >= l + t) {
algs = 0;
if (count_missed(s, t, l, r) > k) {
r--;
continue;
}
bool flag = true;
for (int j = 0; j < n; j++) {
if ((j < l or j > r) and s[j] == '1') {
flag = false;
break;
}
if (!(j >= l and j <= r) and s[j] == '3') algs++;
}
if (!flag) {
r--;
continue;
}
if (algs > max_algs) {
max_algs = algs;
}
r--;
}
l++;
}
cout << max_algs;
}
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 | #include <iostream> using namespace std; int count_meetings(string &s, int l, int r, bool office) { int count = 0; for (int i = l; i < r + 1; i++) { if (s[i] == '1') { count++; } else if (s[i] == '2' and !office) { count++; } } return count; } int count_missed(string &s, int t, int l, int r) { int count = 0; for (int i = 0; i < t; i++) { if (s[i + l] == '1' or s[i + l] == '2') count++; if (s[r - i] == '1' or s[r - i] == '2') count++; } return count; } int main() { int n, k, t, r, l = 0, max_algs = -1, algs; string s; cin >> n >> k >> t >> s; int office_meetings = count_meetings(s, 0, n - 1, true); if (office_meetings <= k) { k -= office_meetings; int home_meetings = 0; for (char c : s) { if (c == '2') home_meetings++; } algs = office_meetings; for (char c : s) { if (c == '3') algs++; } if (home_meetings <= k) { cout << home_meetings + algs; return 0; } else { cout << algs; return 0; } } while (n + 1 - t >= l + t) { r = n - 1; while (r - t >= l + t) { algs = 0; if (count_missed(s, t, l, r) > k) { r--; continue; } bool flag = true; for (int j = 0; j < n; j++) { if ((j < l or j > r) and s[j] == '1') { flag = false; break; } if (!(j >= l and j <= r) and s[j] == '3') algs++; } if (!flag) { r--; continue; } if (algs > max_algs) { max_algs = algs; } r--; } l++; } cout << max_algs; } |
English