#include<cstdio>
#define N 8005
int ile1[N], ile2[N], ile3[N];
//[start, end]
//[1..n]
static int ile(int i, int start, int end) {
if (start <= 0) return 0;
if (start > end) return 0;
if (i == 1) return ile1[end] - ile1[start - 1];
if (i == 2) return ile2[end] - ile2[start - 1];
if (i == 3) return ile3[end] - ile3[start - 1];
if (i == 4) return end - start + 1;
return 0;
}
static int min(int a, int b) {
return a < b ? a : b;
}
static int max(int a, int b) {
return a > b ? a : b;
}
int main() {
int n, k, t;
scanf("%d %d %d\n", &n, &k, &t);
for (int i = 1; i <= n; i++) {
ile1[i] = ile1[i - 1];
ile2[i] = ile2[i - 1];
ile3[i] = ile3[i - 1];
char c = getc(stdin);
if (c == '1') {
ile1[i]++;
}
else if (c == '2') {
ile2[i]++;
}
else if (c == '3') {
ile3[i]++;
}
}
int result = -1;
int ile_spotkan = ile(1, 1, n) + ile(2, 1, n);
if (ile_spotkan <= k) {
result = n;
}
else {
int na_ilu_musze_byc = ile_spotkan - k;
int ile_zdalnych = ile(2, 1, n);
if (ile_zdalnych >= na_ilu_musze_byc) {
result = n - na_ilu_musze_byc;
}
else {
for (int i = 1; i <= n - t + 1; i++) {
for (int j = i + t; j <= n - t + 1; j++) {
int ile_zdalnych = 0;
int ile_w_domu = 0;
int na_ilu_jestem = 0;
// w domu [1, i-1]
ile_zdalnych += ile(2, 1, i - 1);
ile_w_domu += ile(4, 1, i - 1);
// w podróży [i, i+t-1]
// w pracy [i+t, j-1]
na_ilu_jestem += ile(1, i + t, j - 1);
na_ilu_jestem += ile(2, i + t, j - 1);
// w podróży [j, j+t-1]
// w domu [j+t, n]
ile_zdalnych += ile(2, j + t, n);
ile_w_domu += ile(4, j + t, n);
if (na_ilu_jestem >= na_ilu_musze_byc) {
int partial = ile_w_domu;
result = max(result, partial);
}
else {
int na_ile_zdalnych_musze_isc = na_ilu_musze_byc - na_ilu_jestem;
if (ile_zdalnych >= na_ile_zdalnych_musze_isc) {
int partial = ile_w_domu - na_ile_zdalnych_musze_isc;
result = max(result, partial);
}
}
}
}
}
}
printf("%d", result);
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 | #include<cstdio> #define N 8005 int ile1[N], ile2[N], ile3[N]; //[start, end] //[1..n] static int ile(int i, int start, int end) { if (start <= 0) return 0; if (start > end) return 0; if (i == 1) return ile1[end] - ile1[start - 1]; if (i == 2) return ile2[end] - ile2[start - 1]; if (i == 3) return ile3[end] - ile3[start - 1]; if (i == 4) return end - start + 1; return 0; } static int min(int a, int b) { return a < b ? a : b; } static int max(int a, int b) { return a > b ? a : b; } int main() { int n, k, t; scanf("%d %d %d\n", &n, &k, &t); for (int i = 1; i <= n; i++) { ile1[i] = ile1[i - 1]; ile2[i] = ile2[i - 1]; ile3[i] = ile3[i - 1]; char c = getc(stdin); if (c == '1') { ile1[i]++; } else if (c == '2') { ile2[i]++; } else if (c == '3') { ile3[i]++; } } int result = -1; int ile_spotkan = ile(1, 1, n) + ile(2, 1, n); if (ile_spotkan <= k) { result = n; } else { int na_ilu_musze_byc = ile_spotkan - k; int ile_zdalnych = ile(2, 1, n); if (ile_zdalnych >= na_ilu_musze_byc) { result = n - na_ilu_musze_byc; } else { for (int i = 1; i <= n - t + 1; i++) { for (int j = i + t; j <= n - t + 1; j++) { int ile_zdalnych = 0; int ile_w_domu = 0; int na_ilu_jestem = 0; // w domu [1, i-1] ile_zdalnych += ile(2, 1, i - 1); ile_w_domu += ile(4, 1, i - 1); // w podróży [i, i+t-1] // w pracy [i+t, j-1] na_ilu_jestem += ile(1, i + t, j - 1); na_ilu_jestem += ile(2, i + t, j - 1); // w podróży [j, j+t-1] // w domu [j+t, n] ile_zdalnych += ile(2, j + t, n); ile_w_domu += ile(4, j + t, n); if (na_ilu_jestem >= na_ilu_musze_byc) { int partial = ile_w_domu; result = max(result, partial); } else { int na_ile_zdalnych_musze_isc = na_ilu_musze_byc - na_ilu_jestem; if (ile_zdalnych >= na_ile_zdalnych_musze_isc) { int partial = ile_w_domu - na_ile_zdalnych_musze_isc; result = max(result, partial); } } } } } } printf("%d", result); return 0; } |
English