#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define fi first
#define se second
const int MAXN = 8e3 + 7;
int pref[MAXN][3];
void solve(){
int n, k, t;
cin >> n >> k >> t;
string s; cin >> s;
s = "#" + s + "#";
for(int i = 1; i <= n + 1; i++){
pref[i][0] = pref[i - 1][0];
pref[i][1] = pref[i - 1][1];
pref[i][2] = pref[i - 1][2];
if(s[i] == '1') pref[i][0]++;
if(s[i] == '2') pref[i][1]++;
if(s[i] == '3') pref[i][2]++;
}
int ans = -1;
int totalspotkan = pref[n][0] + pref[n][1];
k = min(k, totalspotkan);
// case 1 --> Bajtazar zostaje w domu
int ileleft = k;
if(k - pref[n][0] >= 0){
int wyn = pref[n][0];
for(int i = 1; i <= n; i++){
if(s[i] == '1') continue;
if(s[i] == '2' && ileleft > 0){
wyn++;
ileleft--;
}
else if(s[i] == '3') wyn++;
}
ans = max(ans, wyn);
}
//cout << ans << '\n';
// case 2 --> Bajtazar jedzie do pracy i wraca
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
if(i - t >= 1 && j + t <= n){
// praca --> [i, j]
// dom I --> [0, i - t - 1]
// dom II --> [j + t + 1, n + 1]
pair<int, int> work = {i, j};
pair<int, int> house_one = {0, i - t - 1};
pair<int, int> house_two = {j + t + 1, n + 1};
int ilewolnych = (pref[house_one.se][2]) + (pref[house_two.se][2] - pref[house_two.fi - 1][2]);
int ilespotkan_must = (pref[work.se][1] - pref[work.fi - 1][1]) + (pref[work.se][0] - pref[work.fi - 1][0]);
int ilespotkan_not = (pref[house_one.se][1]) + (pref[house_two.se][1] - pref[house_two.fi - 1][1]);
//int kprev = k;
//k = min(k, totalspotkan);
if(ilespotkan_must + ilespotkan_not >= totalspotkan - k){
if(ilewolnych + (ilespotkan_must + ilespotkan_not + k - totalspotkan) > ans){
ans = max(ans, ilewolnych + (ilespotkan_must + ilespotkan_not + k - totalspotkan));
/* cout << ans << '\n';
cout << i << ' ' << j << '\n';
cout << ilewolnych << '\n';
cout << ilespotkan_must << '\n';
cout << ilespotkan_not << '\n';
cout << totalspotkan << '\n';
cout << "\n\n"; */
}
}
//k = kprev;
}
}
}
cout << ans << '\n';
}
bool multi = 0;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
if(multi) cin >> t;
while(t--) solve();
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 127 128 129 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define pii pair<int, int> #define fi first #define se second const int MAXN = 8e3 + 7; int pref[MAXN][3]; void solve(){ int n, k, t; cin >> n >> k >> t; string s; cin >> s; s = "#" + s + "#"; for(int i = 1; i <= n + 1; i++){ pref[i][0] = pref[i - 1][0]; pref[i][1] = pref[i - 1][1]; pref[i][2] = pref[i - 1][2]; if(s[i] == '1') pref[i][0]++; if(s[i] == '2') pref[i][1]++; if(s[i] == '3') pref[i][2]++; } int ans = -1; int totalspotkan = pref[n][0] + pref[n][1]; k = min(k, totalspotkan); // case 1 --> Bajtazar zostaje w domu int ileleft = k; if(k - pref[n][0] >= 0){ int wyn = pref[n][0]; for(int i = 1; i <= n; i++){ if(s[i] == '1') continue; if(s[i] == '2' && ileleft > 0){ wyn++; ileleft--; } else if(s[i] == '3') wyn++; } ans = max(ans, wyn); } //cout << ans << '\n'; // case 2 --> Bajtazar jedzie do pracy i wraca for(int i = 1; i <= n; i++){ for(int j = i; j <= n; j++){ if(i - t >= 1 && j + t <= n){ // praca --> [i, j] // dom I --> [0, i - t - 1] // dom II --> [j + t + 1, n + 1] pair<int, int> work = {i, j}; pair<int, int> house_one = {0, i - t - 1}; pair<int, int> house_two = {j + t + 1, n + 1}; int ilewolnych = (pref[house_one.se][2]) + (pref[house_two.se][2] - pref[house_two.fi - 1][2]); int ilespotkan_must = (pref[work.se][1] - pref[work.fi - 1][1]) + (pref[work.se][0] - pref[work.fi - 1][0]); int ilespotkan_not = (pref[house_one.se][1]) + (pref[house_two.se][1] - pref[house_two.fi - 1][1]); //int kprev = k; //k = min(k, totalspotkan); if(ilespotkan_must + ilespotkan_not >= totalspotkan - k){ if(ilewolnych + (ilespotkan_must + ilespotkan_not + k - totalspotkan) > ans){ ans = max(ans, ilewolnych + (ilespotkan_must + ilespotkan_not + k - totalspotkan)); /* cout << ans << '\n'; cout << i << ' ' << j << '\n'; cout << ilewolnych << '\n'; cout << ilespotkan_must << '\n'; cout << ilespotkan_not << '\n'; cout << totalspotkan << '\n'; cout << "\n\n"; */ } } //k = kprev; } } } cout << ans << '\n'; } bool multi = 0; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; if(multi) cin >> t; while(t--) solve(); return 0; } |
English