CSES - Datatähti 2022 loppu - Results
Submission details
Task:Peli
Sender:hltk
Submission time:2022-01-22 21:43:50 +0200
Language:C++17
Status:READY
Result:42
Feedback
groupverdictscore
#1ACCEPTED11
#2ACCEPTED31
#30
Test results
testverdicttimegroup
#1ACCEPTED0.03 s1, 2, 3details
#2ACCEPTED0.18 s2, 3details
#3--3details
#4ACCEPTED0.00 s1, 2, 3details
#5ACCEPTED0.01 s1, 2, 3details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:21:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &n, &k);
   ~~~~~^~~~~~~~~~~~~~~~
input/code.cpp:25:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", s);
     ~~~~~^~~~~~~~~

Code

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("arch=skylake")
#include <algorithm>
#include <cstdio>
using namespace std;
int ans;
const int N = 100001;
char s[N];
int t[3][2][51][51];
int &state(int i, int a, int b, int c) {
  if (!a) return t[0][i % 2][b][c];
  if (!b) return t[1][i % 2][a][c];
  return t[2][i % 2][a][b];
}
const int INF = 1e9;
void cmax(int &x, int y) {
  x = max(x, y);
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  if (k < 3) {
    puts("0");
  } else {
    scanf("%s", s);
    for (int a = 0; a <= k; ++a) {
      for (int b = 0; b <= k; ++b) {
        t[0][0][a][b] = -INF;
        t[1][0][a][b] = -INF;
        t[2][0][a][b] = -INF;
        t[0][1][a][b] = -INF;
        t[1][1][a][b] = -INF;
        t[2][1][a][b] = -INF;
      }
    }
    t[0][0][0][0] = 0;
    for (int i = 1; i <= n; ++i) {
      for (int a = 0; a <= k; ++a) {
        for (int b = 0; b <= k - a; ++b) {
          t[0][i % 2][a][b] = t[0][(i + 1) % 2][a][b];
          t[1][i % 2][a][b] = t[1][(i + 1) % 2][a][b];
          t[2][i % 2][a][b] = t[2][(i + 1) % 2][a][b];
        }
      }
      for (int a = 0; a <= k; ++a) {
        for (int b = 0; b <= k - a; ++b) {
          for (int c = 0; c <= k - a - b; ++c) {
            if (a && b && c) break;
            int ps = state(i - 1, a, b, c);
            if (s[i - 1] == 'A') {
              if (b && c && a + b + c < k) {
                cmax(state(i, a, b - 1, c - 1), ps + 1);
              }
              if (a + 1 <= k && a + b + c + 1 <= k && (!b || !c)) {
                cmax(state(i, a + 1, b, c), ps);
              }
            } else if (s[i - 1] == 'B') {
              if (a && c && a + b + c < k) {
                cmax(state(i, a - 1, b, c - 1), ps + 1);
              }
              if (b + 1 <= k && a + b + c + 1 <= k && (!a || !c)) {
                cmax(state(i, a, b + 1, c), ps);
              }
            } else {
              if (a && b && a + b + c < k) {
                cmax(state(i, a - 1, b - 1, c), ps + 1);
              }
              if (c + 1 <= k && a + b + c + 1 <= k && (!a || !b)) {
                cmax(state(i, a, b, c + 1), ps);
              }
            }
          }
        }
      }
      for (int a = 0; a <= k; ++a) {
        for (int b = 0; b <= k - a; ++b) {
          for (int c = 0; c <= k - a - b; ++c) {
            if (a && b && c) break;
            cmax(ans, state(i, a, b, c));
          }
        }
      }
    }
    printf("%d\n", ans);
  }
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
100000 3
BBAACBCBACBACABBCBAABCBCCBCCAA...

correct output
18201

user output
18201

Test 2

Group: 2, 3

Verdict: ACCEPTED

input
100000 10
BAACABCCBCBAACBBCCCCABBBBACCBA...

correct output
29684

user output
29684

Test 3

Group: 3

Verdict:

input
100000 50
ACAABCBBAAAACCBBABACACACBCAACA...

correct output
32740

user output
(empty)

Test 4

Group: 1, 2, 3

Verdict: ACCEPTED

input
3 1
ABC

correct output
0

user output
0

Test 5

Group: 1, 2, 3

Verdict: ACCEPTED

input
3 2
ABC

correct output
0

user output
0