#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define sz(v) ((int)(v).size())
#define vec1d(x) vector<x>
#define vec2d(x) vector<vec1d(x)>
#define vec3d(x) vector<vec2d(x)>
#define vec4d(x) vector<vec3d(x)>
#define ivec1d(x, n, v) vec1d(x)(n, v)
#define ivec2d(x, n, m, v) vec2d(x)(n, ivec1d(x, m, v))
#define ivec3d(x, n, m, k, v) vec3d(x)(n, ivec2d(x, m, k, v))
#define ivec4d(x, n, m, k, l, v) vec4d(x)(n, ivec3d(x, m, k, l, v))
#define nl "\n"
#ifdef LOCAL
#include "pretty_print.hpp"
#define dbg(...) debug_out(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...) 42
#endif
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template <typename T> T sqr(T x) { return x * x; }
template <typename T> T abs(T x) { return x < 0? -x : x; }
template <typename T> T gcd(T a, T b) { return b? gcd(b, a % b) : a; }
template <typename T> bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; }
template <typename T, typename T2> bool chmax(T &x, const T2& y) { if (x < y) { x = y; return true; } return false; }
auto random_address = [] { char *p = new char; return (uint64_t) p; };
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1));
mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1));
const int MAXN = 8042;
short f[MAXN][MAXN][3];
int a[MAXN];
int main(int /* argc */, const char** /* argv */)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k, t;
string s;
while (cin >> n >> k >> t >> s) {
memset(f, 0x80, sizeof(f));
f[0][0][0] = 0;
a[0] = 0;
for (int i = 1; i <= n; ++i) {
a[i] = a[i - 1] + (s[i - 1] != '3');
}
for (int i = 0; i < n; ++i) {
for (int j = k; j >= 0; --j) {
auto t0 = f[i][j][0];
auto t1 = f[i][j][1];
auto t2 = f[i][j][2];
chmax(f[i + 1][j][1], t1);
if (s[i] == '1') {
chmax(f[i + 1][j + 1][0], t0 + 1);
chmax(f[i + 1][j + 1][2], t2 + 1);
} else if (s[i] == '2') {
chmax(f[i + 1][j + 1][0], t0 + 1);
chmax(f[i + 1][j + 1][2], t2 + 1);
chmax(f[i + 1][j][0], t0);
chmax(f[i + 1][j][2], t2);
} else {
chmax(f[i + 1][j][0], t0 + 1);
chmax(f[i + 1][j][2], t2 + 1);
}
}
if (i + 1 >= t) {
int p = i + 1 - t;
auto x = a[i + 1] - a[p];
for (int j = x; j <= k; ++j) {
chmax(f[i + 1][j][1], f[p][j - x][0]);
chmax(f[i + 1][j][2], f[p][j - x][1]);
}
}
}
auto ans = -1;
for (int i = 0; i <= k; ++i) {
chmax(ans, f[n][i][0]);
chmax(ans, f[n][i][2]);
}
cout << ans << nl;
}
#ifdef LOCAL
cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl;
#endif
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin()) #define sz(v) ((int)(v).size()) #define vec1d(x) vector<x> #define vec2d(x) vector<vec1d(x)> #define vec3d(x) vector<vec2d(x)> #define vec4d(x) vector<vec3d(x)> #define ivec1d(x, n, v) vec1d(x)(n, v) #define ivec2d(x, n, m, v) vec2d(x)(n, ivec1d(x, m, v)) #define ivec3d(x, n, m, k, v) vec3d(x)(n, ivec2d(x, m, k, v)) #define ivec4d(x, n, m, k, l, v) vec4d(x)(n, ivec3d(x, m, k, l, v)) #define nl "\n" #ifdef LOCAL #include "pretty_print.hpp" #define dbg(...) debug_out(__LINE__, #__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) 42 #endif typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; template <typename T> T sqr(T x) { return x * x; } template <typename T> T abs(T x) { return x < 0? -x : x; } template <typename T> T gcd(T a, T b) { return b? gcd(b, a % b) : a; } template <typename T> bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename T2> bool chmax(T &x, const T2& y) { if (x < y) { x = y; return true; } return false; } auto random_address = [] { char *p = new char; return (uint64_t) p; }; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); const int MAXN = 8042; short f[MAXN][MAXN][3]; int a[MAXN]; int main(int /* argc */, const char** /* argv */) { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, t; string s; while (cin >> n >> k >> t >> s) { memset(f, 0x80, sizeof(f)); f[0][0][0] = 0; a[0] = 0; for (int i = 1; i <= n; ++i) { a[i] = a[i - 1] + (s[i - 1] != '3'); } for (int i = 0; i < n; ++i) { for (int j = k; j >= 0; --j) { auto t0 = f[i][j][0]; auto t1 = f[i][j][1]; auto t2 = f[i][j][2]; chmax(f[i + 1][j][1], t1); if (s[i] == '1') { chmax(f[i + 1][j + 1][0], t0 + 1); chmax(f[i + 1][j + 1][2], t2 + 1); } else if (s[i] == '2') { chmax(f[i + 1][j + 1][0], t0 + 1); chmax(f[i + 1][j + 1][2], t2 + 1); chmax(f[i + 1][j][0], t0); chmax(f[i + 1][j][2], t2); } else { chmax(f[i + 1][j][0], t0 + 1); chmax(f[i + 1][j][2], t2 + 1); } } if (i + 1 >= t) { int p = i + 1 - t; auto x = a[i + 1] - a[p]; for (int j = x; j <= k; ++j) { chmax(f[i + 1][j][1], f[p][j - x][0]); chmax(f[i + 1][j][2], f[p][j - x][1]); } } } auto ans = -1; for (int i = 0; i <= k; ++i) { chmax(ans, f[n][i][0]); chmax(ans, f[n][i][2]); } cout << ans << nl; } #ifdef LOCAL cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl; #endif return 0; } |
English