#include <iostream> using namespace std; const int NMAX = 8007; int any_work[NMAX]; int office_work[NMAX]; int online_work[NMAX]; int main(){ ios_base::sync_with_stdio(0); int n, k, t; int answer = -1; string s; cin>>n>>k>>t; cin>>s; for(int i=0;i<n;i++){ if(i == 0){ if(s[i] == '1'){ office_work[i] = 1; any_work[i] = 1; } else if(s[i] == '2'){ online_work[i] = 1; any_work[i] = 0; } } else{ office_work[i] = office_work[i-1] + (s[i] == '1' ? 1 : 0); online_work[i] = online_work[i-1] + (s[i] == '2' ? 1 : 0); any_work[i] = any_work[i-1] + (s[i] != '3' ? 1 : 0); } } for(int i=0;i<=n-2*t;i++){ for(int j=i+2*t-1;j<n;j++){ int start = i; int end = j; int missed_work = 0; pair<int,int> traveling[2] ={{start, start + t-1}, {end-t+1, end}}; pair<int,int> home[2] = {{0, start-1}, {end+1, n-1}}; for(int x=0;x<2;x++){ int missed = 0; missed = any_work[traveling[x].second]; if(traveling[x].first > 0){ missed -= any_work[traveling[x].first - 1]; } missed_work += missed; } for(int x=0;x<2;x++){ int missed = 0; if(home[x].first <= home[x].second){ missed = office_work[home[x].second]; if(home[x].first > 0){ missed -= office_work[home[x].first - 1]; } } missed_work += missed; } if(missed_work <= k){ int hours_home_work = 0; int hours_home = 0; for(int x=0;x<2;x++){ if(home[x].first <= home[x].second){ hours_home_work += online_work[home[x].second]; hours_home += (home[x].second - home[x].first + 1); if(home[x].first > 0){ hours_home_work -= online_work[home[x].first - 1]; } } } hours_home_work = max((hours_home_work - (k - missed_work)),0); int programming = hours_home - hours_home_work; answer = max(answer, programming); } } } int office = office_work[n-1]; if(office <= k){ int online = online_work[n-1]; online = max(online - (k - office), 0); answer = max(answer, n - online); } cout<<answer<<"\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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <iostream> using namespace std; const int NMAX = 8007; int any_work[NMAX]; int office_work[NMAX]; int online_work[NMAX]; int main(){ ios_base::sync_with_stdio(0); int n, k, t; int answer = -1; string s; cin>>n>>k>>t; cin>>s; for(int i=0;i<n;i++){ if(i == 0){ if(s[i] == '1'){ office_work[i] = 1; any_work[i] = 1; } else if(s[i] == '2'){ online_work[i] = 1; any_work[i] = 0; } } else{ office_work[i] = office_work[i-1] + (s[i] == '1' ? 1 : 0); online_work[i] = online_work[i-1] + (s[i] == '2' ? 1 : 0); any_work[i] = any_work[i-1] + (s[i] != '3' ? 1 : 0); } } for(int i=0;i<=n-2*t;i++){ for(int j=i+2*t-1;j<n;j++){ int start = i; int end = j; int missed_work = 0; pair<int,int> traveling[2] ={{start, start + t-1}, {end-t+1, end}}; pair<int,int> home[2] = {{0, start-1}, {end+1, n-1}}; for(int x=0;x<2;x++){ int missed = 0; missed = any_work[traveling[x].second]; if(traveling[x].first > 0){ missed -= any_work[traveling[x].first - 1]; } missed_work += missed; } for(int x=0;x<2;x++){ int missed = 0; if(home[x].first <= home[x].second){ missed = office_work[home[x].second]; if(home[x].first > 0){ missed -= office_work[home[x].first - 1]; } } missed_work += missed; } if(missed_work <= k){ int hours_home_work = 0; int hours_home = 0; for(int x=0;x<2;x++){ if(home[x].first <= home[x].second){ hours_home_work += online_work[home[x].second]; hours_home += (home[x].second - home[x].first + 1); if(home[x].first > 0){ hours_home_work -= online_work[home[x].first - 1]; } } } hours_home_work = max((hours_home_work - (k - missed_work)),0); int programming = hours_home - hours_home_work; answer = max(answer, programming); } } } int office = office_work[n-1]; if(office <= k){ int online = online_work[n-1]; online = max(online - (k - office), 0); answer = max(answer, n - online); } cout<<answer<<"\n"; return 0; } |