#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,k,t;
cin >> n >> k >> t;
vector<int> a(n);
vector<int> prefix_office(n + 1);
vector<int> prefix_online(n + 1);
string s;
cin >> s;
for(int i = 0; i < n; i++)
{
a[i] = s[i] - '0';
prefix_office[i + 1] = prefix_office[i];
prefix_online[i + 1] = prefix_online[i];
if(a[i] == 1) prefix_office[i + 1]++;
else if(a[i] == 2) prefix_online[i + 1]++;
}
int maximum = -1;
int total_meetings = prefix_online[n] + prefix_office[n];
k = max(0,total_meetings - k);
for(int i = 0; i <= n - 2*t; i++)
{
for(int j = i + t; j <= n - t; j++)
{
int begin_office = i + t;
int end_office = j;
int leave_home = i;
int come_home = j + t;
int meetings_at_work = prefix_office[end_office] + prefix_online[end_office] -prefix_office[begin_office] - prefix_online[begin_office];
int online_meetings = prefix_online[leave_home] + prefix_online[n] - prefix_online[come_home];
if(meetings_at_work + online_meetings >= k)
{
int online_req = max(0, k - meetings_at_work);
int total = leave_home + n - come_home - online_req;
maximum = max(total, maximum);
}
}
}
if(k <= prefix_online[n])
{
maximum = max(maximum, n - k);
}
cout << maximum;
}
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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k,t; cin >> n >> k >> t; vector<int> a(n); vector<int> prefix_office(n + 1); vector<int> prefix_online(n + 1); string s; cin >> s; for(int i = 0; i < n; i++) { a[i] = s[i] - '0'; prefix_office[i + 1] = prefix_office[i]; prefix_online[i + 1] = prefix_online[i]; if(a[i] == 1) prefix_office[i + 1]++; else if(a[i] == 2) prefix_online[i + 1]++; } int maximum = -1; int total_meetings = prefix_online[n] + prefix_office[n]; k = max(0,total_meetings - k); for(int i = 0; i <= n - 2*t; i++) { for(int j = i + t; j <= n - t; j++) { int begin_office = i + t; int end_office = j; int leave_home = i; int come_home = j + t; int meetings_at_work = prefix_office[end_office] + prefix_online[end_office] -prefix_office[begin_office] - prefix_online[begin_office]; int online_meetings = prefix_online[leave_home] + prefix_online[n] - prefix_online[come_home]; if(meetings_at_work + online_meetings >= k) { int online_req = max(0, k - meetings_at_work); int total = leave_home + n - come_home - online_req; maximum = max(total, maximum); } } } if(k <= prefix_online[n]) { maximum = max(maximum, n - k); } cout << maximum; } |
English