#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int n, k, t;
string s;
int pref1[8005], pref2[8005], pref3[8005];
int main(){
cin>>n>>k>>t;
cin>>s;
pref1[0] = 0;
pref2[0] = 0;
pref3[0] = 0;
int l_spotkan_1 = 0;
int l_spotkan_2 = 0;
for(int i=1; i<=n; i++){
pref1[i] = pref1[i-1] + (s[i-1] == '1');
pref2[i] = pref2[i-1] + (s[i-1] == '2');
pref3[i] = pref3[i-1] + (s[i-1] == '3');
l_spotkan_1 += (s[i-1] == '1');
l_spotkan_2 += (s[i-1] == '2');
}
int l_spotkan = l_spotkan_1 + l_spotkan_2;
int maks = -1;
if(l_spotkan_1 <= k){
maks = max(0, l_spotkan - k);
cout<<n - maks<<endl;
return 0;
}
for(int zdomu = 1; zdomu <= n; zdomu++){
for(int zbiura = zdomu + t; zbiura <= n - t + 1; zbiura++){
int powrot = zbiura + t;
int spotkania_w_biurze = 0;
spotkania_w_biurze += pref1[zbiura-1] - pref1[zdomu+t-1];
spotkania_w_biurze += pref2[zbiura-1] - pref2[zdomu+t-1];
int spotkania_w_domu = 0;
spotkania_w_domu += pref2[n] - pref2[powrot-1];
spotkania_w_domu += pref2[zdomu - 1] - pref2[0];
if(spotkania_w_biurze + spotkania_w_domu >= l_spotkan - k){
int czas_w_domu = zdomu - 1 + n - powrot + 1;
int todo_w_domu = max(0, l_spotkan - k - spotkania_w_biurze);
maks = max(maks, czas_w_domu - todo_w_domu);
}
}
}
cout<<maks<<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 57 58 59 60 | #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int n, k, t; string s; int pref1[8005], pref2[8005], pref3[8005]; int main(){ cin>>n>>k>>t; cin>>s; pref1[0] = 0; pref2[0] = 0; pref3[0] = 0; int l_spotkan_1 = 0; int l_spotkan_2 = 0; for(int i=1; i<=n; i++){ pref1[i] = pref1[i-1] + (s[i-1] == '1'); pref2[i] = pref2[i-1] + (s[i-1] == '2'); pref3[i] = pref3[i-1] + (s[i-1] == '3'); l_spotkan_1 += (s[i-1] == '1'); l_spotkan_2 += (s[i-1] == '2'); } int l_spotkan = l_spotkan_1 + l_spotkan_2; int maks = -1; if(l_spotkan_1 <= k){ maks = max(0, l_spotkan - k); cout<<n - maks<<endl; return 0; } for(int zdomu = 1; zdomu <= n; zdomu++){ for(int zbiura = zdomu + t; zbiura <= n - t + 1; zbiura++){ int powrot = zbiura + t; int spotkania_w_biurze = 0; spotkania_w_biurze += pref1[zbiura-1] - pref1[zdomu+t-1]; spotkania_w_biurze += pref2[zbiura-1] - pref2[zdomu+t-1]; int spotkania_w_domu = 0; spotkania_w_domu += pref2[n] - pref2[powrot-1]; spotkania_w_domu += pref2[zdomu - 1] - pref2[0]; if(spotkania_w_biurze + spotkania_w_domu >= l_spotkan - k){ int czas_w_domu = zdomu - 1 + n - powrot + 1; int todo_w_domu = max(0, l_spotkan - k - spotkania_w_biurze); maks = max(maks, czas_w_domu - todo_w_domu); } } } cout<<maks<<endl; } |
English