Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)
E
Wonderful Teddy Bears
Codeforces
2400
原题
← DF →
翻译中…

You are the proud owner of $n$ teddy bears, which are arranged in a row on a shelf. Each teddy bear is colored either black or pink.

翻译中…

An arrangement of teddy bears is beautiful if all the black teddy bears are to the left of all the pink teddy bears. In other words, there does not exist a pair of indices $(i, j)$ ($1 \leq i \lt j \leq n$) such that the $i$-th teddy bear is pink, and the $j$-th teddy bear is black.

翻译中…

You want to reorder the teddy bears into a beautiful arrangement. You are too short to reach the shelf, but luckily, you can send instructions to a robot to move the teddy bears around. In a single instruction, the robot can:

翻译中…
  • Choose an index $i$ ($1 \le i \le n - 2$) and reorder the teddy bears at positions $i$, $i + 1$ and $i + 2$ so that all the black teddy bears are to the left of all the pink teddy bears.
翻译中…

What is the minimum number of instructions needed to reorder the teddy bears?

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of teddy bears.

翻译中…

The second line of each test case contains a single string $s$ of length $n$ consisting of characters B and P — the colors of the teddy bears. For each $i$ from $1$ to $n$, the $i$-th teddy bear is colored black if $s_i = \texttt{B}$ and pink if $s_i = \texttt{P}$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the minimum number of instructions needed to reorder the teddy bears.

翻译中…

Note

翻译中…

For the first test case, all the teddy bears are pink. Thus, the arrangement is already beautiful, so the answer is $0$.

翻译中…

For the second test case, all the black teddy bears are to the left of all the pink teddy bears. Thus, the answer is $0$.

翻译中…

For the third test case, we can perform $1$ instruction with $i = 1$.

翻译中…

After the instruction, the sequence of colors changes from $\texttt{PPB}$ to $\texttt{BPP}$, and we are done.

翻译中…

For the fourth test case, we can perform $5$ instructions as follows:

翻译中…
  • $i = 1$: $\texttt{}\color{magenta}{\texttt{PPB}}\texttt{PPBB} \rightarrow \texttt{}\color{magenta}{\texttt{BPP}}\texttt{PPBB}$
  • $i = 5$: $\texttt{BPPP}\color{magenta}{\texttt{PBB}}\texttt{} \rightarrow \texttt{BPPP}\color{magenta}{\texttt{BBP}}\texttt{}$
  • $i = 4$: $\texttt{BPP}\color{magenta}{\texttt{PBB}}\texttt{P} \rightarrow \texttt{BPP}\color{magenta}{\texttt{BBP}}\texttt{P}$
  • $i = 3$: $\texttt{BP}\color{magenta}{\texttt{PBB}}\texttt{PP} \rightarrow \texttt{BP}\color{magenta}{\texttt{BBP}}\texttt{PP}$
  • $i = 2$: $\texttt{B}\color{magenta}{\texttt{PBB}}\texttt{PPP} \rightarrow \texttt{B}\color{magenta}{\texttt{BBP}}\texttt{PPP}$
翻译中…

样例

Input #1
5
3
PPP
3
BPP
3
PPB
7
PPBPPBB
15
BPBPBBBBBPBBBBB
Output #1
0
0
1
5
14
InputCopy OutputCopy
题目信息
题号
2096E
时间限制
2 s
内存限制
256 MB
标签
贪心
模拟
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。