#include <bits/stdc++.h>
#define st first
#define nd second
using namespace std;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng_64(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int rand(int l, int r) {return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng_64);}
const int MAX = 8'000;
int dp[MAX+7][MAX+7][3];
int get(int i, int j, int k){
if(j >= 0 && i >= 0) return dp[i][j][k];
return INT_MIN;
}
signed main(){
cin.tie(NULL)->sync_with_stdio(false);
int n, k, t;
cin >> n >> k >> t;
string s;
cin >> s;
for(int j = 0; j <= k; j++){
dp[0][j][1] = INT_MIN;
dp[0][j][2] = INT_MIN;
}
int skips = 0;
for(int i = 1; i <= n; i++){
if(s[i-1] != '3') skips++;
if(i-t-1 >= 0 && s[i-t-1] != '3') skips--;
for(int j = 0; j <= k; j++){
// Jestem w domu (0)
if(s[i-1] == '3') dp[i][j][0] = get(i-1, j, 0) + 1;
else if(s[i-1] == '2') dp[i][j][0] = max(get(i-1, j, 0), get(i-1, j-1, 0) + 1);
else dp[i][j][0] = get(i-1, j-1, 0) + 1;
// Jestem w biurze (1)
dp[i][j][1] = max(get(i-1, j, 1), get(i-t, j-skips, 0));
// Jestem w domu i już byłem w biurze (2)
if(s[i-1] == '3') dp[i][j][2] = get(i-1, j, 2) + 1;
else if(s[i-1] == '2') dp[i][j][2] = max(get(i-1, j, 2), get(i-1, j-1, 2) + 1);
else dp[i][j][2] = get(i-1, j-1, 2) + 1;
dp[i][j][2] = max(dp[i][j][2], get(i-t, j-skips, 1));
}
}
int ans = max(dp[n][k][0], dp[n][k][2]);
if(ans < 0) ans = -1;
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 56 57 58 59 60 61 | #include <bits/stdc++.h> #define st first #define nd second using namespace std; using ld = long double; using ll = long long; using ull = unsigned long long; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rng_64(chrono::high_resolution_clock::now().time_since_epoch().count()); inline int rand(int l, int r) {return uniform_int_distribution<int>(l, r)(rng);} inline ll rand(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng_64);} const int MAX = 8'000; int dp[MAX+7][MAX+7][3]; int get(int i, int j, int k){ if(j >= 0 && i >= 0) return dp[i][j][k]; return INT_MIN; } signed main(){ cin.tie(NULL)->sync_with_stdio(false); int n, k, t; cin >> n >> k >> t; string s; cin >> s; for(int j = 0; j <= k; j++){ dp[0][j][1] = INT_MIN; dp[0][j][2] = INT_MIN; } int skips = 0; for(int i = 1; i <= n; i++){ if(s[i-1] != '3') skips++; if(i-t-1 >= 0 && s[i-t-1] != '3') skips--; for(int j = 0; j <= k; j++){ // Jestem w domu (0) if(s[i-1] == '3') dp[i][j][0] = get(i-1, j, 0) + 1; else if(s[i-1] == '2') dp[i][j][0] = max(get(i-1, j, 0), get(i-1, j-1, 0) + 1); else dp[i][j][0] = get(i-1, j-1, 0) + 1; // Jestem w biurze (1) dp[i][j][1] = max(get(i-1, j, 1), get(i-t, j-skips, 0)); // Jestem w domu i już byłem w biurze (2) if(s[i-1] == '3') dp[i][j][2] = get(i-1, j, 2) + 1; else if(s[i-1] == '2') dp[i][j][2] = max(get(i-1, j, 2), get(i-1, j-1, 2) + 1); else dp[i][j][2] = get(i-1, j-1, 2) + 1; dp[i][j][2] = max(dp[i][j][2], get(i-t, j-skips, 1)); } } int ans = max(dp[n][k][0], dp[n][k][2]); if(ans < 0) ans = -1; cout << ans << '\n'; } |
English