#include<bits/stdc++.h>
#define st first
#define nd second
#define all(x) x.begin(), x.end()
#define BOOST cin.tie(NULL); ios_base::sync_with_stdio(false);
// #define int ll
typedef long long ll;
using namespace std;
template <typename T> struct tag:reference_wrapper <T>{ using reference_wrapper <T>::reference_wrapper; };
template <typename T1, typename T2> static inline tag <ostream> operator<<(tag <ostream> os, pair<T1, T2> const& p){ return os.get()<<"{"<<p.first<<", "<<p.second<<"}", os;}
template <typename Other> static inline tag <ostream> operator<<(tag <ostream> os, Other const& o){ os.get()<<o; return os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, vector <T> const& v){ os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, set <T> const& s){ vector <T> v; for (auto i: s) v.push_back(i); os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }
int n, k, t;
vector<int> p12(n + 2, 0);
vector<int> p2(n + 2, 0);
vector<int> p1(n + 2, 0);
int c12(int l, int r){return p12[r] - p12[l - 1];}
int c1(int l, int r){return p1[r] - p1[l - 1];}
int c2(int l, int r){return p2[r] - p2[l - 1];}
int calc(int sb, int eb){
int miss = 0;
int pot = 0;
int cal2 = 0;
miss = c1(1, sb - 1) + c12(sb, sb + t - 1) + c12(eb, eb + t - 1) + c1(eb + t, n);
if(miss > k) return -1;
pot = sb - 1 - c2(1, sb - 1) + (n - (eb + t - 1)) - c2(eb + t, n);
cal2 = c2(1, sb - 1) + c2(eb + t, n);
return pot + min((k - miss), cal2);
}
int32_t main(){
BOOST;
cin >> n >> k >> t;
string day; cin >> day;
day = "#" + day + "###";
p12.assign(n + 2, 0);
p2.assign(n + 2, 0);
p1.assign(n + 2, 0);
for(int i = 1; i <= n + 1; i++){
p12[i] = p12[i - 1];
p2[i] = p2[i - 1];
p1[i] = p1[i - 1];
if(day[i] == '1'){
p12[i]++;
p1[i]++;
}
if(day[i] == '2'){
p12[i]++;
p2[i]++;
}
}
int ans = -1;
if(p12[n] - p2[n] <= k){
ans = n - max(p12[n] - k, 0);
}
for(int i = 1; i <= n; i++){
for(int j = i + t; j <= n - t + 1; j++){
ans = max(ans, calc(i,j));
}
}
cout << ans << "\n";
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 | #include<bits/stdc++.h> #define st first #define nd second #define all(x) x.begin(), x.end() #define BOOST cin.tie(NULL); ios_base::sync_with_stdio(false); // #define int ll typedef long long ll; using namespace std; template <typename T> struct tag:reference_wrapper <T>{ using reference_wrapper <T>::reference_wrapper; }; template <typename T1, typename T2> static inline tag <ostream> operator<<(tag <ostream> os, pair<T1, T2> const& p){ return os.get()<<"{"<<p.first<<", "<<p.second<<"}", os;} template <typename Other> static inline tag <ostream> operator<<(tag <ostream> os, Other const& o){ os.get()<<o; return os; } template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, vector <T> const& v){ os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; } template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, set <T> const& s){ vector <T> v; for (auto i: s) v.push_back(i); os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; } int n, k, t; vector<int> p12(n + 2, 0); vector<int> p2(n + 2, 0); vector<int> p1(n + 2, 0); int c12(int l, int r){return p12[r] - p12[l - 1];} int c1(int l, int r){return p1[r] - p1[l - 1];} int c2(int l, int r){return p2[r] - p2[l - 1];} int calc(int sb, int eb){ int miss = 0; int pot = 0; int cal2 = 0; miss = c1(1, sb - 1) + c12(sb, sb + t - 1) + c12(eb, eb + t - 1) + c1(eb + t, n); if(miss > k) return -1; pot = sb - 1 - c2(1, sb - 1) + (n - (eb + t - 1)) - c2(eb + t, n); cal2 = c2(1, sb - 1) + c2(eb + t, n); return pot + min((k - miss), cal2); } int32_t main(){ BOOST; cin >> n >> k >> t; string day; cin >> day; day = "#" + day + "###"; p12.assign(n + 2, 0); p2.assign(n + 2, 0); p1.assign(n + 2, 0); for(int i = 1; i <= n + 1; i++){ p12[i] = p12[i - 1]; p2[i] = p2[i - 1]; p1[i] = p1[i - 1]; if(day[i] == '1'){ p12[i]++; p1[i]++; } if(day[i] == '2'){ p12[i]++; p2[i]++; } } int ans = -1; if(p12[n] - p2[n] <= k){ ans = n - max(p12[n] - k, 0); } for(int i = 1; i <= n; i++){ for(int j = i + t; j <= n - t + 1; j++){ ans = max(ans, calc(i,j)); } } cout << ans << "\n"; return 0; } |
English