#include<bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"{"<<p.first<<", "<<p.second<<"}";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X...) cerr<<"["#X"]: ", [](auto...$) {((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif #define mp(x, y) make_pair(x, y) #define fi first #define se second #define eb emplace_back int32_t main() { cin.tie(0)->sync_with_stdio(0); int n, k, t; cin>>n>>k>>t; string s; cin>>s; vector<int> of(n); vector<int> ho(n); for(int i=0;i<n;i++) of[i] += (i != 0 ? of[i-1] : 0) + (s[i] == '1' ? 1 : 0); for(int i=0;i<n;i++) ho[i] += (i != 0 ? ho[i-1] : 0) + (s[i] == '2' ? 1 : 0); int tar = max(ho.back() + of.back() - k, 0); int result = -1; if(ho.back() >= tar) result = n - tar; for(int i=t;i<n;i++) for(int j=i;j+t<n;j++) { int pew = of[j] - of[i-1] + ho[j] - ho[i-1]; if(pew + (i-t-1 >= 0 ? ho[i-t-1] : 0) + ho[n-1] - ho[j+t] < tar) continue; result = max(result, i - t + (n-1) - (j+t) - (tar - pew)); } cout<<result; }
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 | #include<bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"{"<<p.first<<", "<<p.second<<"}";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X...) cerr<<"["#X"]: ", [](auto...$) {((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif #define mp(x, y) make_pair(x, y) #define fi first #define se second #define eb emplace_back int32_t main() { cin.tie(0)->sync_with_stdio(0); int n, k, t; cin>>n>>k>>t; string s; cin>>s; vector<int> of(n); vector<int> ho(n); for(int i=0;i<n;i++) of[i] += (i != 0 ? of[i-1] : 0) + (s[i] == '1' ? 1 : 0); for(int i=0;i<n;i++) ho[i] += (i != 0 ? ho[i-1] : 0) + (s[i] == '2' ? 1 : 0); int tar = max(ho.back() + of.back() - k, 0); int result = -1; if(ho.back() >= tar) result = n - tar; for(int i=t;i<n;i++) for(int j=i;j+t<n;j++) { int pew = of[j] - of[i-1] + ho[j] - ho[i-1]; if(pew + (i-t-1 >= 0 ? ho[i-t-1] : 0) + ho[n-1] - ho[j+t] < tar) continue; result = max(result, i - t + (n-1) - (j+t) - (tar - pew)); } cout<<result; } |