CSES - Datatähti 2022 loppu - Results
Submission details
Task:Peli
Sender:MojoLake
Submission time:2024-10-09 20:06:59 +0300
Language:C++ (C++20)
Status:READY
Result:42
Feedback
groupverdictscore
#1ACCEPTED11
#2ACCEPTED31
#30
Test results
testverdicttimegroup
#1ACCEPTED0.39 s1, 2, 3details
#2ACCEPTED0.86 s2, 3details
#30.00 s3details
#4ACCEPTED0.00 s1, 2, 3details
#5ACCEPTED0.00 s1, 2, 3details

Code

#include <bits/stdc++.h>

#define all(x) begin(x), end(x)

using namespace std;
using ll = long long;

const int N = 1e5 + 5;
const int K = 11;
const int inf = 1e9;

int dp[N][K][K][K]; // i, a, b, c

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    string s; cin >> s;
    s = "#" + s;
    if (k > 10) return 0;

    for (int i = 0; i <= n; ++i) {
        for (int a = 0; a <= k; ++a) {
            for (int b = 0; b <= k; ++b) {
                for (int c = 0; c <= k; ++c) {
                    dp[i][a][b][c] = -inf;
                }
            }
        }
    }


    dp[0][0][0][0] = 0;

    for (int i = 1; i <= n; ++i) {
        char ch = s[i];
        for (int a = 0; a <= k; ++a) {
            for (int b = 0; a + b <= k; ++b) {
                for (int c = 0; a + b + c <= k; ++c) {
                    assert(a + b + c <= k);

                    // a, b, c are from the previous
                    if (dp[i-1][a][b][c] < 0)continue;
                    int pre = dp[i-1][a][b][c];
                    dp[i][a][b][c] = max(dp[i][a][b][c], pre);

                    if (a + b + c == k) continue; // can't take more
                    int na = a + (int)(ch == 'A');
                    int nb = b + (int)(ch == 'B');
                    int nc = c + (int)(ch == 'C');
                    int mn = min({na, nb, nc});

                    if (mn > 0) {
                        dp[i][na-1][nb-1][nc-1] = max(dp[i][na-1][nb-1][nc-1], pre + 1);
                    }

                    dp[i][na][nb][nc] = max(dp[i][na][nb][nc], pre);
                    
                }
            }
        }
        // cout << i << " " << ans << "\n";
    }

    int ans = 0;
    for (int i = 0; i <= n; ++i) {
        for (int a = 0; a <= k; ++a) {
            for (int b = 0; b <= k; ++b) {
                for (int c = 0; c <= k; ++c) {
                    if (a + b + c > k)continue;
                    ans = max(ans, dp[i][a][b][c]);
                }
            }
        }
    }

    cout << ans << "\n";
}

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