CSES - Datatähti 2022 loppu - Results
Submission details
Task:Peli
Sender:MojoLake
Submission time:2024-10-09 21:08:19 +0300
Language:C++ (C++20)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.70 s1, 2, 3details
#20.70 s2, 3details
#30.70 s3details
#40.70 s1, 2, 3details
#50.70 s1, 2, 3details

Code

#include <bits/stdc++.h>

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

using namespace std;
using ll = long long;

template<typename S, typename T = S> void chmax(S &s, T t) {s = s > t ? s : t;}

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

// int dp[N][K][K][3]; // i, 0: [a][b], 1: [a][c], 2: [b][c]
vector<vector<vector<vector<int>>>> dp(
        N, vector<vector<vector<int>>>(K, vector<vector<int>>(K, vector<int>(3)))
    );

int n, k;

void rm(int i, int a, int b, int c, int pre) {
    assert(a > 0 && b > 0 && c > 0);

    if (a == 1) {
        int m = 2; // [b][c]
        chmax(dp[i][b-1][c-1][m], pre + 1);
    }

    if (b == 1) {
        int m  = 1; // [a][c]
        chmax(dp[i][a-1][c-1][m], pre + 1);
    }

    if (c == 1) {
        int m = 0; // [a][b]
        chmax(dp[i][a-1][b-1][m], pre + 1);
    }
}

void m0(int i, int a, int b, char ch) {
    int m = 0; // [a][b]
    int pre = dp[i-1][a][b][m];
    if (pre < 0)return; // previous state impossible

    // always: (not taking current character)
    chmax(dp[i][a][b][m], pre);

    if (a + b == k) return; // can't take current character

    if (ch == 'A') {
        chmax(dp[i][a+1][b][m], pre);
        return;
    } 

    if (ch == 'B') {
        chmax(dp[i][a][b+1][m], pre);
        return;
    } 
    // else ch == 'C'
    int c = 1;

    int mn = min({a, b, c});

    if (mn > 0) {
        rm(i, a, b, c, pre); // try removing different stuff
        return;
    }

    // now either a == 0 or b == 0
    assert(a == 0 || b == 0);

    if (a == 0) {
        m = 2; // [b][c]
        chmax(dp[i][b][c][m], pre);
    }

    if (b == 0) {
        m = 1; // [a][c]
        chmax(dp[i][a][c][m], pre);
    }
}

void m1(int i, int a, int c, char ch) {
    int m = 1; // [a][c]
    int pre = dp[i-1][a][c][m];
    if (pre < 0)return; // previous state impossible

    // always: (not taking current character)
    chmax(dp[i][a][c][m], pre);

    if (a + c == k) return; // can't take current character

    if (ch == 'A') {
        chmax(dp[i][a+1][c][m], pre);
        return;
    } 

    if (ch == 'C') {
        chmax(dp[i][a][c+1][m], pre);
        return;
    } 
    // else ch == 'B'
    int b = 1;

    int mn = min({a, b, c});

    if (mn > 0) {
        rm(i, a, b, c, pre); // try removing different stuff
        return;
    }

    // now either a == 0 or c == 0
    assert(a == 0 || c == 0);

    if (a == 0) {
        m = 2; // [b][c]
        chmax(dp[i][b][c][m], pre);
    }

    if (c == 0) {
        m = 0; // [a][b]
        chmax(dp[i][a][b][m], pre);
    }
}

void m2(int i, int b, int c, char ch) {
    int m = 2; // [b][c]
    int pre = dp[i-1][b][c][m];
    if (pre < 0)return; // previous state impossible

    // always: (not taking current character)
    chmax(dp[i][b][c][m], pre);

    if (b + c == k) return; // can't take current character

    if (ch == 'B') {
        chmax(dp[i][b+1][c][m], pre);
        return;
    } 

    if (ch == 'C') {
        chmax(dp[i][b][c+1][m], pre);
        return;
    } 
    // else ch == 'A'
    int a = 1;

    int mn = min({a, b, c});

    if (mn > 0) {
        rm(i, a, b, c, pre); // try removing different stuff
        return;
    }

    // now either b == 0 or c == 0
    assert(b == 0 || c == 0);

    if (b == 0) {
        m = 1; // [a][c]
        chmax(dp[i][a][c][m], pre);
    }

    if (c == 0) {
        m = 0; // [a][b]
        chmax(dp[i][a][b][m], pre);
    }
}

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

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

    dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
    int ans = 0;

    for (int i = 1; i <= n; ++i) {
        char ch = s[i];
        for (int e = 0; e <= k; ++e) {
            for (int f = 0; e + f <= k; ++f) {
                for (int m = 0; m < 3; ++m) {
                    m0(i, e, f, ch);
                    m1(i, e, f, ch);
                    m2(i, e, f, ch);
                }
            }
        }
    }
    
    for (int i = 1; i <= n; ++i) {
        for (int a = 0; a <= k; ++a) {
            for (int b = 0; a + b <= k; ++b) {
                for (int m = 0; m < 3; ++m) {
                    chmax(ans, dp[i][a][b][m]);
                }
            }
        }
    }
    
    cout << ans << "\n";

}

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
100000 3
BBAACBCBACBACABBCBAABCBCCBCCAA...

correct output
18201

user output
(empty)

Test 2

Group: 2, 3

Verdict:

input
100000 10
BAACABCCBCBAACBBCCCCABBBBACCBA...

correct output
29684

user output
(empty)

Test 3

Group: 3

Verdict:

input
100000 50
ACAABCBBAAAACCBBABACACACBCAACA...

correct output
32740

user output
(empty)

Test 4

Group: 1, 2, 3

Verdict:

input
3 1
ABC

correct output
0

user output
(empty)

Test 5

Group: 1, 2, 3

Verdict:

input
3 2
ABC

correct output
0

user output
(empty)