/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 8ms 576.0 KiB
#4 Accepted 10ms 564.0 KiB
#5 Accepted 17ms 564.0 KiB
#6 Accepted 28ms 612.0 KiB
#7 Accepted 37ms 772.0 KiB
#8 Accepted 14ms 564.0 KiB
#9 Accepted 29ms 560.0 KiB
#10 Accepted 35ms 564.0 KiB
#11 Accepted 28ms 564.0 KiB
#12 Accepted 34ms 640.0 KiB
#13 Accepted 37ms 652.0 KiB
#14 Accepted 30ms 636.0 KiB
#15 Accepted 133ms 776.0 KiB
#16 Accepted 24ms 776.0 KiB
#17 Accepted 152ms 876.0 KiB
#18 Accepted 172ms 880.0 KiB
#19 Accepted 39ms 876.0 KiB
#20 Accepted 152ms 904.0 KiB
#21 Accepted 113ms 876.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;

/*#ifndef ONLINE_JUDGE
#include "DEBUG.h"
#define bug(...)           __f (#__VA_ARGS__, __VA_ARGS__)
#endif*/

#define first_in_out       ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int              long long int
#define double             long double
#define print(a)           for(auto x : a) cout << x << " ";
#define printpair(a)       for(auto x : a) cout << x.first << " " << x.second<<"\n";

int n, k;
string s;

int chk(int mid)
{
	int x = 0, a = 0;
	int res = 0;

	for (int i = 0; i < n; i++)
	{

		if (s[i] == 'A')
			a++;
		else if (s[i] == 'B')
			res += a;
		else if (s[i] == '?') {
			if (mid <= 0)
				continue;
			x++;
			a++, mid--;
		}
	}

	int rem = k - x;
	for (int i = n - 1; i >= 0; i--)
	{

		if (s[i] == 'A')
			a--;
		else if (s[i] == '?')
		{
			if (rem <= 0)
				continue;

			rem--;
			res += a;
		}

	}

	return res;
}



void solve()
{

	cin >> n >> k;
	cin >> s;

	int q = count(s.begin(), s.end(), '?');
	k = min(k, q);

	int left = 0, right = k, mid1, mid2, ans = chk(0);

	while (right - left + 1 >= 3)
	{
		mid1 = left + (right - left) / 3;
		mid2 = right - (right - left) / 3;

		int aa = chk(mid1);
		int bb = chk(mid2);

		if (aa > bb)
			right = mid2 - 1;
		else
			left = mid1 + 1;


	}

	for (int i = left; i <= right; i++)
		ans = max(ans, chk(i));

	cout << ans << "\n";




}


int32_t main()
{
	first_in_out

	int t = 1;
	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-11-08 16:19:49
Judged At
2024-11-11 02:22:41
Judged By
Score
100
Total Time
172ms
Peak Memory
904.0 KiB