/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 9ms 580.0 KiB
#4 Accepted 11ms 532.0 KiB
#5 Accepted 17ms 596.0 KiB
#6 Accepted 26ms 752.0 KiB
#7 Accepted 29ms 1.617 MiB
#8 Accepted 13ms 532.0 KiB
#9 Accepted 25ms 580.0 KiB
#10 Accepted 28ms 532.0 KiB
#11 Accepted 17ms 532.0 KiB
#12 Accepted 22ms 900.0 KiB
#13 Accepted 24ms 1.008 MiB
#14 Accepted 31ms 1.062 MiB
#15 Accepted 85ms 2.18 MiB
#16 Accepted 33ms 2.414 MiB
#17 Accepted 98ms 2.773 MiB
#18 Accepted 120ms 2.77 MiB
#19 Accepted 38ms 3.766 MiB
#20 Accepted 89ms 2.77 MiB
#21 Accepted 69ms 2.863 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using minheap = priority_queue<long long, vector<long long>, greater<long long>>;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define ll long long
#define ld long double
#define MOD 1000000007
#define pie 2*(acos(0.0))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define pb push_back
#define endl '\n'
#define lcm(a,b) (a*b)/(__gcd<ll>(a,b))
#define print(v) for(auto e:v) cout<<e<<" "; cout<<endl;
#define printp(v) for(auto e:v) cout<<e.first<<" "<<e.second<<endl;
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define life_is_a_race ios::sync_with_stdio(false); cin.tie(nullptr);

void solve(int tc)
{
    //cout<<"Case "<<tc<<": ";
    ll n,k; cin >> n >> k;
    string s; cin >> s;
    ll a=0,b=0,q=0;
    ll right[n][3],leftA[n];

    for(int i=n-1;i>=0;i--)
    {
        right[i][0] = a;
        right[i][1] = b;
        right[i][2] = q;
        a += (s[i]=='A');
        b += (s[i]=='B');
        q += (s[i]=='?');
    }

    a=0; b=0; q=0;
    for(int i=0;i<n;i++)
    {
        leftA[i] = a;
        if(s[i]=='A') ++a;
        else if(s[i]=='B') ++b;
        else if(s[i]=='?')
        {
            if(right[i][2] >= k) continue;
            if(!k) continue;
            if(a < right[i][1]) ++a, s[i]='A',--k;
            else
            {
                if(right[i][2] >= k) continue;
                else
                {
                    s[i] = 'B'; ++b; --k;
                }
            }
        }
    }

    ll ans = 0;
    for(int i=n-1;i>=0;i--)
    {
        if(s[i]=='B') ans += leftA[i];
    }
    cout << ans << endl;
}

ll countans(ll mid,ll k,ll n,string s,vector<ll>&lftA,vector<ll>&qtn)
{
    ll ans = 0,c=0,a=0;
    for(int i=0;i<n;i++)
    {
        lftA[i] = a;
        if(s[i]=='?' && mid!=-1 && i<=qtn[mid]) s[i]='A', ++c, ++a;
        else if(s[i]=='A') ++a;
    }
    for(int i=n-1;i>=0;i--)
    {
        if(s[i]=='B') ans += lftA[i];
        else if(s[i]=='?' && k-c) s[i]='B', ++c , ans += lftA[i];
    }
    return ans;
}

void solve2()
{
    ll n,k; cin >> n >> k;
    string s; cin >> s;
    vector<ll> qtn;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='?') qtn.push_back(i);
    }

    ll lo=0,hi=min(k-1,(ll)qtn.size()),mid,ans=0;
    ll lt,rt,md;
    vector<ll>lftA(n);
    while(lo<=hi)
    {
        mid = (lo+hi)/2;
        lt = 0; rt = 0;
        lt = countans(mid-1,k,n,s,lftA,qtn);
        md = countans(mid,k,n,s,lftA,qtn);
        if(mid < min(k-1,(ll)qtn.size())) rt = countans(mid+1,k,n,s,lftA,qtn);
        ans = max({ans,lt,md,rt});
        if(lt < md) lo=mid+1;
        else if(rt < md) hi = mid-1;
        else if(rt > md) lo = mid+1;
        else if(lt > md) hi = mid-1;
        else break;
    }
    cout << ans << endl;
}

int main()
{
    life_is_a_race
    int t=1; 
    cin>>t;
    for(int i=1;i<=t;i++) solve2();
}

Information

Submit By
Type
Submission
Problem
P1110 Subsequence of AB
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-12 07:21:02
Judged At
2024-10-12 07:21:02
Judged By
Score
100
Total Time
120ms
Peak Memory
3.766 MiB