#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int suma(const vector<int> &pref, int l, int r)
{
if (r < 0)
return 0;
return pref[r] - (l > 0 ? pref[l-1] : 0);
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
int n, k, t;
cin >> n >> k >> t;
string s;
cin >> s;
vector<int> pref1(n), pref2(n), pref3(n);
pref1[0] = (s[0] == '1');
for (int i = 1; i < n; i++)
pref1[i] = pref1[i-1] + (s[i] == '1');
pref2[0] = (s[0] == '2');
for (int i = 1; i < n; i++)
pref2[i] = pref2[i-1] + (s[i] == '2');
pref3[0] = (s[0] == '3');
for (int i = 1; i < n; i++)
pref3[i] = pref3[i-1] + (s[i] == '3');
int wszystkie_spotkania = suma(pref2, 0, n-1) + suma(pref1, 0, n-1);
int potrzebne_spotkania = max(0, wszystkie_spotkania-k);
int ans = -1;
for (int l = t; l < n-t; l++)
{
for (int r = l; r < n-t; r++)
{
// w czasie [l, r] jest w biurze
// w czasie [0, l-t-1] u [r+t+1, n-1] jest w domu
int spotkania_w_domu = suma(pref2, 0, l-t-1) + suma(pref2, r+t+1, n-1);
int spotkania_w_biurze = suma(pref2, l, r) + suma(pref1, l, r);
int potrzebne_spotkania_w_domu = potrzebne_spotkania - spotkania_w_biurze;
if (potrzebne_spotkania_w_domu > spotkania_w_domu)
continue;
int czas_w_biurze = r-l+1;
int czas_drogi = 2*t;
int czas_w_domu = n-czas_w_biurze-czas_drogi;
int czas_wolny = czas_w_domu - potrzebne_spotkania_w_domu;
ans = max(ans, czas_wolny);
}
}
int spotkania_w_domu = suma(pref2, 0, n-1);
if (spotkania_w_domu >= potrzebne_spotkania)
ans = max(ans, n-potrzebne_spotkania);
cout << ans << "\n";
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; int suma(const vector<int> &pref, int l, int r) { if (r < 0) return 0; return pref[r] - (l > 0 ? pref[l-1] : 0); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k, t; cin >> n >> k >> t; string s; cin >> s; vector<int> pref1(n), pref2(n), pref3(n); pref1[0] = (s[0] == '1'); for (int i = 1; i < n; i++) pref1[i] = pref1[i-1] + (s[i] == '1'); pref2[0] = (s[0] == '2'); for (int i = 1; i < n; i++) pref2[i] = pref2[i-1] + (s[i] == '2'); pref3[0] = (s[0] == '3'); for (int i = 1; i < n; i++) pref3[i] = pref3[i-1] + (s[i] == '3'); int wszystkie_spotkania = suma(pref2, 0, n-1) + suma(pref1, 0, n-1); int potrzebne_spotkania = max(0, wszystkie_spotkania-k); int ans = -1; for (int l = t; l < n-t; l++) { for (int r = l; r < n-t; r++) { // w czasie [l, r] jest w biurze // w czasie [0, l-t-1] u [r+t+1, n-1] jest w domu int spotkania_w_domu = suma(pref2, 0, l-t-1) + suma(pref2, r+t+1, n-1); int spotkania_w_biurze = suma(pref2, l, r) + suma(pref1, l, r); int potrzebne_spotkania_w_domu = potrzebne_spotkania - spotkania_w_biurze; if (potrzebne_spotkania_w_domu > spotkania_w_domu) continue; int czas_w_biurze = r-l+1; int czas_drogi = 2*t; int czas_w_domu = n-czas_w_biurze-czas_drogi; int czas_wolny = czas_w_domu - potrzebne_spotkania_w_domu; ans = max(ans, czas_wolny); } } int spotkania_w_domu = suma(pref2, 0, n-1); if (spotkania_w_domu >= potrzebne_spotkania) ans = max(ans, n-potrzebne_spotkania); cout << ans << "\n"; } |
English