/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 1ms 324.0 KiB
#3 Accepted 43ms 580.0 KiB
#4 Accepted 42ms 532.0 KiB
#5 Accepted 54ms 680.0 KiB
#6 Accepted 61ms 1.676 MiB
#7 Accepted 74ms 6.215 MiB
#8 Accepted 38ms 572.0 KiB
#9 Accepted 69ms 604.0 KiB
#10 Accepted 71ms 652.0 KiB
#11 Accepted 65ms 656.0 KiB
#12 Accepted 45ms 2.336 MiB
#13 Accepted 73ms 2.816 MiB
#14 Accepted 62ms 2.816 MiB
#15 Accepted 97ms 7.547 MiB
#16 Accepted 163ms 10.602 MiB
#17 Accepted 113ms 14.395 MiB
#18 Accepted 120ms 14.758 MiB
#19 Accepted 234ms 21.23 MiB
#20 Accepted 110ms 14.488 MiB
#21 Accepted 108ms 14.258 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
void solve()
{
   int n , k , K;
   string s ; cin >> n >> k >> s ; K = k ;
   ll dp[n + 5] = {0} , a = 0 , b = 0;
   map < char , int > mp;
   map < int , int > isQ , prevA ;
   vector < int > v ;
   for(int i = 0 ; i < n ; i++) if(s[i] == '?') isQ[i]++ ;
   int i = n - 1 ;
// changing all question mark ? to B from ending
   while( k > 0 && i >= 0){
     if(s[i] == '?') {
        s[i] = 'B' ;
        k-- ;
     }
     i-- ;
   }
   // mapping all position "B" which i has been changed from "?"
   for(int i = 0 ; i < n ; i++){
    if(s[i] == 'A') a++ ;
    else if(isQ[i]) prevA[i] = a ;
    if(isQ[i] && s[i] == 'B') v.pb(i) ;
   }
   // mapping result for initial string which i changed before
   dp[0] = 0;
   if(s[0] == 'A') mp['A']++ ;
   if(s[0] == 'B' && isQ[0]) mp['B']++ ;
   if(s[0] == '?') mp['?']++ ;
   for(int i = 1 ; i < n ; i++){
    if(s[i] == 'B'){
        dp[i] = dp[i - 1] + mp['A'] ;
        if(mp['A'] || mp['?'] || isQ[i] || mp['B']) mp['B']++ ;
    }
    else if(s[i] == 'A') mp['A']++ ;
    else if(s[i] == '?') mp['?']++ ;
    if(s[i] != 'B') dp[i] = dp[i - 1] ;
   }

  a = 0 ; bool ok = 0 ;
  ll Ans = dp[n - 1] , changeToA = 0 , j = 0 , take = 0 , changA = 0 , cnt = 0;
  for(int i = 0 ; i < n ; i++){
    // when I am getting "?" then switching it to "A" to get maximum result
    if(s[i] == '?') {
        ok = 1 ;
    if(j < v.size()){
        ll cur = Ans ;
        ll prevAFound = prevA[v[j]] + changeToA ;
        cur  -= prevAFound ;
        cur += (mp['B'] - 1);
        // checking maximum
        if(cur > Ans){
            Ans = cur ;
            changeToA++ ;
            mp['B']-- ;
            take++ ;
            changA++ ;
            j++ ;
            cnt++ ;
        }
    }
    }
  // changing "B" to "A" to get maximum result , "B" which I changed from "?"
    else if(isQ[i] && cnt < K) {
        ll cur = Ans + mp['B'] - 1 ;
        // checking the position of "B" for removing
        int in = lower_bound(v.begin() , v.end() , i) - v.begin() ;
        // if i choose "?" to get maximum Ans then i have to remove  freq of "?" th position of "B" which i store into vector V.
        in += changA ;
        if(in >= v.size()) in = v.size() - 1 ;
        ll prevAFound = prevA[v[in]] + changeToA ;
        cur -= prevAFound;
        // checking maximum
         if(cur > Ans){
            Ans = cur ;
            cnt++ ;
            take-- ;
            changeToA++;
            ok = 1 ;
            mp['B']-- ;
         }
         else if(take > 0) {
            take-- ;
         } else mp['B']-- ;
    }
    else if(s[i] == 'A') a++ , ok = 1;
    else if(s[i] == 'B' && ok) mp['B']-- ;
  }
  cout << Ans << endl ;
}
int main()
{
     __Heart__
     int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1110 Subsequence of AB
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-12 00:11:32
Judged At
2024-11-11 02:37:52
Judged By
Score
100
Total Time
234ms
Peak Memory
21.23 MiB