/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 324.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 31ms 556.0 KiB
#4 Accepted 10ms 532.0 KiB
#5 Accepted 7ms 532.0 KiB
#6 Accepted 9ms 872.0 KiB
#7 Accepted 10ms 2.086 MiB
#8 Accepted 11ms 532.0 KiB
#9 Accepted 12ms 592.0 KiB
#10 Accepted 11ms 580.0 KiB
#11 Accepted 11ms 592.0 KiB
#12 Accepted 7ms 1.043 MiB
#13 Accepted 10ms 1.156 MiB
#14 Accepted 8ms 948.0 KiB
#15 Accepted 12ms 2.227 MiB
#16 Accepted 9ms 2.234 MiB
#17 Accepted 13ms 3.832 MiB
#18 Accepted 13ms 3.781 MiB
#19 Accepted 10ms 3.883 MiB
#20 Accepted 13ms 3.898 MiB
#21 Accepted 13ms 3.871 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-11 02:30:19
Judged By
Score
100
Total Time
31ms
Peak Memory
3.898 MiB