#include <iostream>
#include <vector>
using namespace std;
struct Count : vector< int > {
Count(int v, string const &s) : vector< int >(1 + s.size()) {
int i, c = 0;
for (i = 0; i < (int)s.size(); ++i)
(*this)[1 + i] = (c += (s[i] == v));
}
int count(int from, int to) const {
return (*this)[to] - (*this)[from];
}
int count() const {
return (*this)[this->size() - 1];
}
};
struct Pra {
int n, k, t;
Count c1, c2, c3;
Pra(int n, int k, int t, string const &s)
: n(n), k(k), t(t), c1('1', s), c2('2', s), c3('3', s) {
}
int solve() const {
if (k >= c1.count())
return n - (c2.count() - min(c2.count(), k - c1.count()));
int result = -1;
int start = 0;
while (start + t + t <= n) {
int end = start + t;
while (++end + t <= n)
result = max(result, solve(start, end));
++start;
}
return result;
}
int solve(int start, int stop) const {
int c1_skipped = c1.count(0, start + t) + c1.count(stop, n);
int c2_skipped = c2.count(start, start + t) + c2.count(stop, stop + t);
int c2_skip = k - (c1_skipped + c2_skipped);
if (c2_skip < 0)
return -1;
int count = c1.count(0, start) + c1.count(stop + t, n) + c3.count(0, start) + c3.count(stop + t, n);
int c2_count = c2.count(0, start) + c2.count(stop + t, n);
if (c2_skip > c2_count)
c2_skip = c2_count;
count += c2_skip;
return count;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k, t;
string s;
cin >> n >> k >> t >> s;
cout << Pra(n, k, t, s).solve() << '\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 | #include <iostream> #include <vector> using namespace std; struct Count : vector< int > { Count(int v, string const &s) : vector< int >(1 + s.size()) { int i, c = 0; for (i = 0; i < (int)s.size(); ++i) (*this)[1 + i] = (c += (s[i] == v)); } int count(int from, int to) const { return (*this)[to] - (*this)[from]; } int count() const { return (*this)[this->size() - 1]; } }; struct Pra { int n, k, t; Count c1, c2, c3; Pra(int n, int k, int t, string const &s) : n(n), k(k), t(t), c1('1', s), c2('2', s), c3('3', s) { } int solve() const { if (k >= c1.count()) return n - (c2.count() - min(c2.count(), k - c1.count())); int result = -1; int start = 0; while (start + t + t <= n) { int end = start + t; while (++end + t <= n) result = max(result, solve(start, end)); ++start; } return result; } int solve(int start, int stop) const { int c1_skipped = c1.count(0, start + t) + c1.count(stop, n); int c2_skipped = c2.count(start, start + t) + c2.count(stop, stop + t); int c2_skip = k - (c1_skipped + c2_skipped); if (c2_skip < 0) return -1; int count = c1.count(0, start) + c1.count(stop + t, n) + c3.count(0, start) + c3.count(stop + t, n); int c2_count = c2.count(0, start) + c2.count(stop + t, n); if (c2_skip > c2_count) c2_skip = c2_count; count += c2_skip; return count; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, t; string s; cin >> n >> k >> t >> s; cout << Pra(n, k, t, s).solve() << '\n'; } |
English