/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 1ms 768.0 KiB
#3 Accepted 31ms 796.0 KiB
#4 Wrong Answer 37ms 332.0 KiB
#5 Accepted 51ms 540.0 KiB
#6 Accepted 66ms 1.703 MiB
#7 Wrong Answer 90ms 6.219 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 ,SS; cin >> n >> k >> s ; SS = 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 ;
   while( k > 0 && i >= 0){
     if(s[i] == '?') {
        s[i] = 'B' ;
        k-- ;
     }
     i-- ;
   }
   //---------------------------------
   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) ;
   }
   dp[0] = 0;
 //  cout << "bug ==> " <<  s << endl ;
   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] ;
   }
//   for(int i = 0 ; i < n ; i++) cout << dp[i] << " " ; cout << endl ;
  a = 0 ; bool ok = 0 ;
  ll Ans = dp[n - 1] , changeToA = 0 , j = 0 , take = 0 , cnt = 0;
  for(int i = 0 ; i < n ; i++){
    if(s[i] == '?') {
        ok = 1 ;
    if(j < v.size()){
        ll cur = Ans - prevA[v[j]] - changeToA ;
        cur += mp['B'] - 1;
        if(cur > Ans){
            Ans = cur ;
            changeToA++ ;
            mp['B']-- ;
            take++ ;
            cnt++ ;
            j++ ;
        }
    }
    }
    else if(isQ[i] && cnt < K) {
        ll cur = Ans + mp['B'] - 1;
        if(take <= 0) cur -= a ;
        else if(take > 0) cur -= (prevA[v[j]] + 2);
     //  cout << i << " " << cur << " " << take << " " << a << " " << mp['B'] << " " << Ans << endl ;
         if(cur > Ans && take < K){
            Ans = cur ;
            a++ ;
            cnt++ ;
            if(take > 0)a++ ;
            take-- ;
            ok = 1 ;
            mp['B']-- ;
         }
    }
    else if(s[i] == 'A') a++ , ok = 1;
    else if(s[i] == 'B' && ok) mp['B']-- ;
    //cout << i << " " << Ans << endl ;
  }
  cout <<  Ans << endl ;
}
int main()
{
     __Heart__
    // READ("0in.txt") ;
   // WRITE("0out.txt") ;
     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-10 13:47:14
Judged At
2024-10-10 13:47:14
Judged By
Score
8
Total Time
90ms
Peak Memory
6.219 MiB