#include <vector>
#include <map>
#include <string>
#include <cstdio>
#include <iostream>
#include <ostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned char ubyte;
constexpr ld EPS = 1e-9;
constexpr ld PI = 3.141592653589793238462643383279502884L;
constexpr int INF = (int)1e9 + 5;
constexpr ll LINF = (ll)1e18 + 5;
bool eq(ld a, ld b) {
return fabs(a - b) < EPS;
}
template<class T>
T sqr(const T & x) { return x * x; }
template<class T>
ll abs(const T & x) { return x < 0 ? -x : x; }
template<class T>
ll myround(const T & x) { return x < 0 ? x - 0.5 : x + 0.5; }
template<class T>
bool chmin(T & x, const T & y) {
if (y < x) {
x = y;
return true;
}
return false;
}
template<class T>
ostream & operator <<(ostream & os, const vector<T> & v) {
os << "{";
for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
os << *it;
if ((it + 1) != v.end()) os << ", ";
}
os << "}";
return os;
}
template<class P, class Q>
ostream & operator <<(ostream & os, const pair<P, Q> & p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template<class T>
bool chmax(T & x, const T & y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template<class IntegerType = int>
IntegerType nextInt() {
IntegerType x = 0;
bool p = false;
char c;
do {
c = getchar();
} while (c <= 32);
if (c == '-') {
p = true;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return (p ? -x : x);
}
string nextToken() {
static const size_t MAX_LEN = 500500;
static char str[MAX_LEN];
scanf("%s", str);
return str;
}
//template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
//template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
#define all(a) (a).begin(), (a).end()
void no_solution()
{
puts("-1");
exit(0);
}
vector<int> calc(string s, char c)
{
int n = (int)s.size();
vector<int> a(n + 1);
a[0] = 0;
for ( int i = 0; i < n; i++ )
{
a[i + 1] = a[i];
if ( s[i] == c )
a[i + 1] += 1;
}
return a;
}
void testcase()
{
int n = nextInt<int>();
int k = nextInt<int>();
int t = nextInt<int>();
string s = nextToken();
vector<int> a1 = calc(s, '1');
vector<int> a2 = calc(s, '2');
vector<int> a3 = calc(s, '3');
auto segment = [&](int l, int r, int& a, int& b, int& c)
{
b = a2[r + 1] - a2[l];
a = a1[r + 1] - a1[l];
c = a3[r + 1] - a3[l];
};
auto best_ans = [&](int& res, int newres)
{
if ( res == -1 )
res = newres;
else if ( newres > res )
res = newres;
};
int spotkanie_w_biurze;
int zdalne_spotkanie;
int brak_obowiazkow;
segment(0, n - 1, spotkanie_w_biurze, zdalne_spotkanie, brak_obowiazkow);
int spotkania = zdalne_spotkanie + spotkanie_w_biurze;
//std::cout << spotkania << std::endl;
int res = -1;
if ( spotkanie_w_biurze <= k )
{
int cur = brak_obowiazkow + spotkanie_w_biurze;
int skip_spotkanie = std::min(k - spotkanie_w_biurze, zdalne_spotkanie);
cur += skip_spotkanie;
best_ans(res, cur);
}
for (int i = 0; i + t <= n; i++)
{
for ( int j = i + t; j + t <= n; j++ )
{
int spotkanie_w_biurze1;
int zdalne_spotkanie1;
int brak_obowiazkow1;
int spotkanie_w_biurze2;
int zdalne_spotkanie2;
int brak_obowiazkow2;
int spotkanie_w_biurze3;
int zdalne_spotkanie3;
int brak_obowiazkow3;
segment(0, i - 1, spotkanie_w_biurze1, zdalne_spotkanie1, brak_obowiazkow1);
segment(i + t, j - 1, spotkanie_w_biurze2, zdalne_spotkanie2, brak_obowiazkow2);
segment(j + t, n - 1, spotkanie_w_biurze3, zdalne_spotkanie3, brak_obowiazkow3);
spotkanie_w_biurze1 += spotkanie_w_biurze3;
zdalne_spotkanie1 += zdalne_spotkanie3;
brak_obowiazkow1 += brak_obowiazkow3;
int tot_attending = zdalne_spotkanie1 + spotkanie_w_biurze2 + zdalne_spotkanie2;
//std::cout << i << " " << j << " " << tot_attending << std::endl;
if ( tot_attending < spotkania - k )
continue;
int cur = spotkanie_w_biurze1 + brak_obowiazkow1;
int toadd = std::min(tot_attending - (spotkania - k), zdalne_spotkanie1);
cur += toadd;
best_ans(res, cur);
}
}
printf("%d\n", res);
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int t = 1;
while (t--)
testcase();
}
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | #include <vector> #include <map> #include <string> #include <cstdio> #include <iostream> #include <ostream> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef unsigned char ubyte; constexpr ld EPS = 1e-9; constexpr ld PI = 3.141592653589793238462643383279502884L; constexpr int INF = (int)1e9 + 5; constexpr ll LINF = (ll)1e18 + 5; bool eq(ld a, ld b) { return fabs(a - b) < EPS; } template<class T> T sqr(const T & x) { return x * x; } template<class T> ll abs(const T & x) { return x < 0 ? -x : x; } template<class T> ll myround(const T & x) { return x < 0 ? x - 0.5 : x + 0.5; } template<class T> bool chmin(T & x, const T & y) { if (y < x) { x = y; return true; } return false; } template<class T> ostream & operator <<(ostream & os, const vector<T> & v) { os << "{"; for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) { os << *it; if ((it + 1) != v.end()) os << ", "; } os << "}"; return os; } template<class P, class Q> ostream & operator <<(ostream & os, const pair<P, Q> & p) { return os << "(" << p.first << ", " << p.second << ")"; } template<class T> bool chmax(T & x, const T & y) { if (x < y) { x = y; return true; } return false; } template<class IntegerType = int> IntegerType nextInt() { IntegerType x = 0; bool p = false; char c; do { c = getchar(); } while (c <= 32); if (c == '-') { p = true; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return (p ? -x : x); } string nextToken() { static const size_t MAX_LEN = 500500; static char str[MAX_LEN]; scanf("%s", str); return str; } //template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; //template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; #define all(a) (a).begin(), (a).end() void no_solution() { puts("-1"); exit(0); } vector<int> calc(string s, char c) { int n = (int)s.size(); vector<int> a(n + 1); a[0] = 0; for ( int i = 0; i < n; i++ ) { a[i + 1] = a[i]; if ( s[i] == c ) a[i + 1] += 1; } return a; } void testcase() { int n = nextInt<int>(); int k = nextInt<int>(); int t = nextInt<int>(); string s = nextToken(); vector<int> a1 = calc(s, '1'); vector<int> a2 = calc(s, '2'); vector<int> a3 = calc(s, '3'); auto segment = [&](int l, int r, int& a, int& b, int& c) { b = a2[r + 1] - a2[l]; a = a1[r + 1] - a1[l]; c = a3[r + 1] - a3[l]; }; auto best_ans = [&](int& res, int newres) { if ( res == -1 ) res = newres; else if ( newres > res ) res = newres; }; int spotkanie_w_biurze; int zdalne_spotkanie; int brak_obowiazkow; segment(0, n - 1, spotkanie_w_biurze, zdalne_spotkanie, brak_obowiazkow); int spotkania = zdalne_spotkanie + spotkanie_w_biurze; //std::cout << spotkania << std::endl; int res = -1; if ( spotkanie_w_biurze <= k ) { int cur = brak_obowiazkow + spotkanie_w_biurze; int skip_spotkanie = std::min(k - spotkanie_w_biurze, zdalne_spotkanie); cur += skip_spotkanie; best_ans(res, cur); } for (int i = 0; i + t <= n; i++) { for ( int j = i + t; j + t <= n; j++ ) { int spotkanie_w_biurze1; int zdalne_spotkanie1; int brak_obowiazkow1; int spotkanie_w_biurze2; int zdalne_spotkanie2; int brak_obowiazkow2; int spotkanie_w_biurze3; int zdalne_spotkanie3; int brak_obowiazkow3; segment(0, i - 1, spotkanie_w_biurze1, zdalne_spotkanie1, brak_obowiazkow1); segment(i + t, j - 1, spotkanie_w_biurze2, zdalne_spotkanie2, brak_obowiazkow2); segment(j + t, n - 1, spotkanie_w_biurze3, zdalne_spotkanie3, brak_obowiazkow3); spotkanie_w_biurze1 += spotkanie_w_biurze3; zdalne_spotkanie1 += zdalne_spotkanie3; brak_obowiazkow1 += brak_obowiazkow3; int tot_attending = zdalne_spotkanie1 + spotkanie_w_biurze2 + zdalne_spotkanie2; //std::cout << i << " " << j << " " << tot_attending << std::endl; if ( tot_attending < spotkania - k ) continue; int cur = spotkanie_w_biurze1 + brak_obowiazkow1; int toadd = std::min(tot_attending - (spotkania - k), zdalne_spotkanie1); cur += toadd; best_ans(res, cur); } } printf("%d\n", res); } int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int t = 1; while (t--) testcase(); } |
English