/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 332.0 KiB
#2 Accepted 2ms 332.0 KiB
#3 Accepted 10ms 576.0 KiB
#4 Accepted 9ms 772.0 KiB
#5 Accepted 13ms 556.0 KiB
#6 Accepted 24ms 612.0 KiB
#7 Accepted 36ms 772.0 KiB
#8 Accepted 11ms 564.0 KiB
#9 Accepted 21ms 568.0 KiB
#10 Accepted 24ms 564.0 KiB
#11 Accepted 15ms 560.0 KiB
#12 Accepted 18ms 636.0 KiB
#13 Accepted 23ms 648.0 KiB
#14 Accepted 18ms 628.0 KiB
#15 Accepted 87ms 772.0 KiB
#16 Accepted 19ms 676.0 KiB
#17 Accepted 110ms 876.0 KiB
#18 Accepted 138ms 880.0 KiB
#19 Accepted 38ms 876.0 KiB
#20 Accepted 95ms 876.0 KiB
#21 Accepted 63ms 872.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-11 02:27:05
Judged By
Score
100
Total Time
138ms
Peak Memory
880.0 KiB