#include <bits/stdc++.h>
// #define int long long
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k, t;
cin >> n >> k >> t;
vector<int> prefixBiuro(n + 1, 0);
vector<int> prefixZdalne(n + 1, 0);
vector<int> prefixPotyczki(n + 1, 0);
for (int i = 0; i < n; i++)
{
char x;
cin >> x;
prefixBiuro[i + 1] = prefixBiuro[i];
prefixZdalne[i + 1] = prefixZdalne[i];
prefixPotyczki[i + 1] = prefixPotyczki[i];
if (x == '1')
prefixBiuro[i + 1]++;
else if (x == '2')
prefixZdalne[i + 1]++;
else
prefixPotyczki[i + 1]++;
}
// for (auto p : prefixBiuro)
// cerr << p << " ";
// cerr << endl;
// for (auto p : prefixZdalne)
// cerr << p << " ";
// cerr << endl;
// for (auto p : prefixPotyczki)
// cerr << p << " ";
// cerr << endl;
int bestZadania = -1;
// case 1. zostaje w domu
int opuszczone = prefixBiuro[n]; // ile dni nie moge pracowac
if (opuszczone <= k)
{
bestZadania = prefixPotyczki[n] + min(prefixBiuro[n] + prefixZdalne[n], k);
}
// case 2-... jedzie do biura raz
// i - dzień wyjazdu do biura
// i + t - pierwszy dzień w biurze
// j - dzień wyjazdu do domu
// j + t - pierwszy dzień w domu (potencjalnie = n)
for (int i = 0; i < n - 2 * t; i++)
{
for (int j = i + t + 1; j + t <= n; j++)
{
opuszczone = 0; // ile dni nie moge pracowac
int wagary = 0; // ile dni potencjalnie moge isc na wagary (zeby robic zadanka)
int wolne = 0; // ile dni poswiecam na zadania za free
// segment 1. jest w domu [0; i - 1]
if (i != 0)
{
opuszczone += prefixBiuro[i];
wolne += prefixBiuro[i] + prefixPotyczki[i];
wagary += prefixZdalne[i];
}
// segment 2. jest w biurze [i + t; j - 1]
// W BIURZE NIE MOŻE ROBIĆ ZADAŃ!
// wolne += prefixPotyczki[j] - prefixPotyczki[i + t];
// wagary += (prefixBiuro[j] - prefixBiuro[i + t]) + (prefixZdalne[j] - prefixZdalne[i + t]);
// segment 3. jest w domu [j + t; n - 1]
if (j + t != n)
{
opuszczone += prefixBiuro[n] - prefixBiuro[j + t];
wolne += (prefixBiuro[n] - prefixBiuro[j + t]) + (prefixPotyczki[n] - prefixPotyczki[j + t]);
// cerr << "Dodaje wolne = " << wolne << endl;
// cerr << "Opuszczone = " << opuszczone << endl;
wagary += prefixZdalne[n] - prefixZdalne[j + t];
}
// segment A. jedzie dom -> biuro [i; i + t - 1]
opuszczone += (prefixBiuro[i + t] - prefixBiuro[i]) + (prefixZdalne[i + t] - prefixZdalne[i]);
// segment B. jedzie biuro -> dom [j; j + t - 1]
opuszczone += (prefixBiuro[j + t] - prefixBiuro[j]) + (prefixZdalne[j + t] - prefixZdalne[j]);
// cerr << "Opuszczone = " << opuszczone << endl;
if (opuszczone > k)
continue;
int wynik = wolne + min(wagary, k - opuszczone);
// cerr << i << " " << j << " " << wolne << " " << wynik << endl;
if (wynik > bestZadania)
{
cerr << i << " " << j << endl;
}
bestZadania = max(bestZadania, wynik);
}
}
cout << bestZadania << endl;
}
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include <bits/stdc++.h> // #define int long long using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, t; cin >> n >> k >> t; vector<int> prefixBiuro(n + 1, 0); vector<int> prefixZdalne(n + 1, 0); vector<int> prefixPotyczki(n + 1, 0); for (int i = 0; i < n; i++) { char x; cin >> x; prefixBiuro[i + 1] = prefixBiuro[i]; prefixZdalne[i + 1] = prefixZdalne[i]; prefixPotyczki[i + 1] = prefixPotyczki[i]; if (x == '1') prefixBiuro[i + 1]++; else if (x == '2') prefixZdalne[i + 1]++; else prefixPotyczki[i + 1]++; } // for (auto p : prefixBiuro) // cerr << p << " "; // cerr << endl; // for (auto p : prefixZdalne) // cerr << p << " "; // cerr << endl; // for (auto p : prefixPotyczki) // cerr << p << " "; // cerr << endl; int bestZadania = -1; // case 1. zostaje w domu int opuszczone = prefixBiuro[n]; // ile dni nie moge pracowac if (opuszczone <= k) { bestZadania = prefixPotyczki[n] + min(prefixBiuro[n] + prefixZdalne[n], k); } // case 2-... jedzie do biura raz // i - dzień wyjazdu do biura // i + t - pierwszy dzień w biurze // j - dzień wyjazdu do domu // j + t - pierwszy dzień w domu (potencjalnie = n) for (int i = 0; i < n - 2 * t; i++) { for (int j = i + t + 1; j + t <= n; j++) { opuszczone = 0; // ile dni nie moge pracowac int wagary = 0; // ile dni potencjalnie moge isc na wagary (zeby robic zadanka) int wolne = 0; // ile dni poswiecam na zadania za free // segment 1. jest w domu [0; i - 1] if (i != 0) { opuszczone += prefixBiuro[i]; wolne += prefixBiuro[i] + prefixPotyczki[i]; wagary += prefixZdalne[i]; } // segment 2. jest w biurze [i + t; j - 1] // W BIURZE NIE MOŻE ROBIĆ ZADAŃ! // wolne += prefixPotyczki[j] - prefixPotyczki[i + t]; // wagary += (prefixBiuro[j] - prefixBiuro[i + t]) + (prefixZdalne[j] - prefixZdalne[i + t]); // segment 3. jest w domu [j + t; n - 1] if (j + t != n) { opuszczone += prefixBiuro[n] - prefixBiuro[j + t]; wolne += (prefixBiuro[n] - prefixBiuro[j + t]) + (prefixPotyczki[n] - prefixPotyczki[j + t]); // cerr << "Dodaje wolne = " << wolne << endl; // cerr << "Opuszczone = " << opuszczone << endl; wagary += prefixZdalne[n] - prefixZdalne[j + t]; } // segment A. jedzie dom -> biuro [i; i + t - 1] opuszczone += (prefixBiuro[i + t] - prefixBiuro[i]) + (prefixZdalne[i + t] - prefixZdalne[i]); // segment B. jedzie biuro -> dom [j; j + t - 1] opuszczone += (prefixBiuro[j + t] - prefixBiuro[j]) + (prefixZdalne[j + t] - prefixZdalne[j]); // cerr << "Opuszczone = " << opuszczone << endl; if (opuszczone > k) continue; int wynik = wolne + min(wagary, k - opuszczone); // cerr << i << " " << j << " " << wolne << " " << wynik << endl; if (wynik > bestZadania) { cerr << i << " " << j << endl; } bestZadania = max(bestZadania, wynik); } } cout << bestZadania << endl; } |
English