#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,k,t;
cin >> n>>k>>t;
int lsp=0;
vector<int> stationary(n+1,0), online(n+1,0), free(n+1,0);
FOR(i,1,n+1){
char a;
cin >>a;
if(a=='1') {stationary[i]=1; lsp++;}
if(a=='2') {online[i]=1; lsp++;}
if(a=='3') free[i]=1;
stationary[i]+=stationary[i-1];
online[i]+=online[i-1];
free[i]+=free[i-1];
}
int wyn=-1;
if(online[n]>=max(0,lsp-k)){
wyn=n-max(0,lsp-k);
}
// for(auto i :stationary) cout << i << ' ';
// cout << endl;
// for(auto i :online) cout << i << ' ';
// cout << endl;
// for(auto i :free) cout << i << ' ';
// cout << endl;
FOR(beg,t+1,n-t+1){
FOR(en,beg,n-t+1){
int spotkania=stationary[en]-stationary[beg-1] + online[en]-online[beg-1];
int lpotsp=max(0,lsp-k-spotkania);
if(online[beg-t-1] + online[n]-online[en+t] >= lpotsp){
wyn=max(wyn,beg-t-1 + n-en-t - lpotsp);
}
}
}
cout << wyn;
}
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 | #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=a;i<b;i++) int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k,t; cin >> n>>k>>t; int lsp=0; vector<int> stationary(n+1,0), online(n+1,0), free(n+1,0); FOR(i,1,n+1){ char a; cin >>a; if(a=='1') {stationary[i]=1; lsp++;} if(a=='2') {online[i]=1; lsp++;} if(a=='3') free[i]=1; stationary[i]+=stationary[i-1]; online[i]+=online[i-1]; free[i]+=free[i-1]; } int wyn=-1; if(online[n]>=max(0,lsp-k)){ wyn=n-max(0,lsp-k); } // for(auto i :stationary) cout << i << ' '; // cout << endl; // for(auto i :online) cout << i << ' '; // cout << endl; // for(auto i :free) cout << i << ' '; // cout << endl; FOR(beg,t+1,n-t+1){ FOR(en,beg,n-t+1){ int spotkania=stationary[en]-stationary[beg-1] + online[en]-online[beg-1]; int lpotsp=max(0,lsp-k-spotkania); if(online[beg-t-1] + online[n]-online[en+t] >= lpotsp){ wyn=max(wyn,beg-t-1 + n-en-t - lpotsp); } } } cout << wyn; } |
English