/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 10ms 568.0 KiB
#4 Accepted 10ms 564.0 KiB
#5 Accepted 16ms 572.0 KiB
#6 Accepted 23ms 644.0 KiB
#7 Accepted 29ms 740.0 KiB
#8 Accepted 12ms 564.0 KiB
#9 Accepted 22ms 580.0 KiB
#10 Accepted 25ms 632.0 KiB
#11 Accepted 12ms 628.0 KiB
#12 Accepted 12ms 536.0 KiB
#13 Accepted 20ms 656.0 KiB
#14 Accepted 6ms 576.0 KiB
#15 Accepted 58ms 936.0 KiB
#16 Accepted 17ms 1.066 MiB
#17 Accepted 66ms 1.109 MiB
#18 Accepted 113ms 1.73 MiB
#19 Accepted 35ms 2.777 MiB
#20 Accepted 61ms 1.074 MiB
#21 Accepted 37ms 1.055 MiB

Code

#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif

using namespace std;
#define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';

void shelby() {
    int n, k;
    string s;
    cin >> n >> k >> s;
    vector<int> id;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '?') {
            id.push_back(i);
            if (id.size() == k) break;
        }
    }
    auto calc = [&](int x)-> int {
        string tmp = s;
        for (int i = 0; i < (x == id.size() ? (id.empty() ? n : id.back() + 1) : id[x]); ++i) {
            if (tmp[i] == '?') tmp[i] = 'A';
        }
        int rem = k - x;
        for (int i = n - 1; i >= 0 && rem; --i) {
            if (tmp[i] == '?') tmp[i] = 'B', rem--;
        }
        int b = count(tmp.begin(), tmp.end(), 'B'), ret = 0;
        for (int i = 0; i < n; ++i) {
            if (tmp[i] == 'A') ret += b;
            else if (tmp[i] == 'B') b--;
        }
        debug(tmp, ret);

        return ret;
    };
    debug(id);
    int l = 0, r = id.size(), ans = 0;
    while (l <= r) {
        int m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
        int calc1 = calc(m1), calc2 = calc(m2);
        if (calc1 >= calc2) ans = max(ans, calc1), r = m2 - 1;
        else ans = max(ans, calc2), l = m1 + 1;
    }
    cout << ans << '\n';
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1110 Subsequence of AB
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-06 17:23:28
Judged At
2024-11-11 02:24:19
Judged By
Score
100
Total Time
113ms
Peak Memory
2.777 MiB