/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 30ms 532.0 KiB
#4 Accepted 9ms 532.0 KiB
#5 Accepted 7ms 532.0 KiB
#6 Accepted 10ms 916.0 KiB
#7 Accepted 13ms 2.262 MiB
#8 Accepted 15ms 536.0 KiB
#9 Accepted 24ms 532.0 KiB
#10 Accepted 21ms 532.0 KiB
#11 Accepted 11ms 592.0 KiB
#12 Accepted 7ms 1008.0 KiB
#13 Accepted 12ms 932.0 KiB
#14 Accepted 16ms 948.0 KiB
#15 Accepted 25ms 2.242 MiB
#16 Accepted 14ms 2.27 MiB
#17 Accepted 20ms 3.91 MiB
#18 Accepted 20ms 3.66 MiB
#19 Accepted 15ms 3.66 MiB
#20 Accepted 20ms 3.66 MiB
#21 Accepted 16ms 3.785 MiB

Code

#include<bits/stdc++.h>
using namespace std; 
char nl = '\n';
using i64 = long long;

void solve(int t) {
    // cout << "test #" << t << nl;
    i64 n, k; cin >> n >> k;
    string str; cin >> str;
    vector<i64> cnt1(n + 1), cnt2(n + 1);
    i64 tot = 0, already = 0;
    i64 x = 0;
    i64 q = 0;
    for (auto& a : str) {
        tot += a == 'B';
    }
    for (int i = 0; i < n; i++) {
        if (str[i] == 'B') tot--;
        if (str[i] == '?') {
            q++;
            x++;
            already += tot;
            cnt1[x] = already;
        }
    }
    k = min(k, q);
    reverse(str.begin(), str.end());
    tot = 0; already = 0, x = 0;
    for (auto& a : str) {
        tot += a == 'A';
    }
    for (int i = 0; i < n; i++) {
        if (str[i] == 'A') tot--;
        if (str[i] == '?') {
            x++;
            already += tot;
            cnt2[x] = already;
        }
    }
    reverse(str.begin(), str.end());
    i64 ans = 0;
    for (i64 i = 0; i <= k; i++) {
        ans = max(ans, i * (k - i) + cnt1[i] + cnt2[k - i]);
        // cout << cnt1[i] << " " << cnt2[k - i] << nl;
    }
    tot = 0, already = 0, x = 0;
    for (int i = 0; i < n; i++) {
        tot += str[i] == 'B';
    }
    for (int i = 0; i < n; i++) {
        if (str[i] == 'A') {
            ans += tot;
        } else if (str[i] == 'B') {
            tot -= 1;
        }
    }
    cout << ans << nl;
} 

int main() {
    int tt = 1;
    cin >> tt;
    for(int t = 1; t <= tt; t++) solve(t);
}

Information

Submit By
Type
Submission
Problem
P1110 Subsequence of AB
Contest
Brain Booster #7
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-05 15:38:33
Judged At
2024-11-05 15:38:33
Judged By
Score
100
Total Time
30ms
Peak Memory
3.91 MiB