/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 3ms 532.0 KiB
#3 Accepted 6ms 532.0 KiB
#4 Accepted 9ms 532.0 KiB
#5 Accepted 13ms 324.0 KiB
#6 Accepted 19ms 532.0 KiB
#7 Accepted 34ms 532.0 KiB
#8 Accepted 9ms 568.0 KiB
#9 Accepted 16ms 568.0 KiB
#10 Accepted 18ms 532.0 KiB
#11 Accepted 11ms 532.0 KiB
#12 Accepted 14ms 532.0 KiB
#13 Accepted 18ms 616.0 KiB
#14 Accepted 33ms 604.0 KiB
#15 Accepted 73ms 532.0 KiB
#16 Accepted 11ms 532.0 KiB
#17 Accepted 81ms 788.0 KiB
#18 Accepted 109ms 788.0 KiB
#19 Accepted 21ms 788.0 KiB
#20 Accepted 76ms 788.0 KiB
#21 Accepted 52ms 868.0 KiB

Code

#define _GLIBCXX_FILESYSTEM
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

int n,k,q;
string s;

ll f(int x) {
    int now = 0, cnt = 0;
    ll tot = 0;
    for(int i = 0; i < n; i++) {
        if(s[i] == '?') {
            if(now < x) cnt++, now++;
        } else if(s[i] == 'A') cnt++;
        else if(s[i] == 'B') tot += 1ll * cnt;
    }
    for(int i = n - 1; i >= 0; i--) {
        if(s[i] == 'A') cnt--;
        else if(s[i] == '?') {
            if(now >= q) break;
            now++;
            tot += 1ll * cnt;
        }
    }
    return tot;
}

void solve() {
    cin >> n >> k >> s;
    q = 0;
    for(auto c: s) if(c == '?') q++;
    q = min(q, k);
    int l = 0, r = q, m1,m2;
    ll ans = 0;
    while(r - l + 1 >= 3) {
        m1 = l + (r - l) / 3;
        m2 = r - (r - l) / 3;
        ll f1 = f(m1), f2 = f(m2);
        if(f1 > f2) r = m2 - 1;
        else l = m1 + 1;
    } 
    for(int i = l; i <= r; i++) {
        ans = max(ans, f(i));
    }
    cout << ans << '\n';
    return;
}

int32_t main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tc = 1;
    cin >> tc;
    for(int kase = 1; kase <= tc; kase++) {
        //cout << "Case " << kase << ": ";
        solve();
    }
    return 0;
}

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 16:31:16
Judged At
2024-11-05 16:31:16
Judged By
Score
100
Total Time
109ms
Peak Memory
868.0 KiB