AtCoder Grand Contest 072
B
AGC Language
AtCoder
⌀ 3148
原题
← AC →
翻译中…

Problem Statement

翻译中…

An AGC word is a string of length at least $2$ that contains the same number of A’s and C’s, and in every prefix, A’s make up at least half of the characters. You are given a string $S$ of length $2N$ consisting of $N$ A’s and $N$ C’s. For each $k = 1, 2, \dots, K$, answer the following question.

翻译中…

A blackboard shows a string that is a concatenation of $k$ copies of $S$. To turn it into an AGC word, perform the operations below in the order written:

  1. Perform exactly once:
  • Choose integers $(l, r)$ $(1 \le l \le r \le 2kN)$ and reverse the $l$-th through $r$-th characters of the string.
  1. Perform zero or more times:
  • Swap two adjacent characters of the string.

What is the minimum number of swaps required?

翻译中…

Notes on prefix A string $Y$ is a prefix of $X$ if and only if $1 \le |Y| \le |X|$ and the first $|Y|$ characters of $X$ equal $Y$. For example, ATC is a prefix of ATCODER, but AGC and ATCODERGRANDCONTEST are not.

翻译中…

Constraints

翻译中…
  • $1 \le N \le 1\,000\,000$
  • $1 \le K \le 300\,000$
  • $S$ is a length‑$2N$ string consisting of $N$ A’s and $N$ C’s.
  • $N$ and $K$ are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N K
S

翻译中…

Output

翻译中…

Print $K$ lines. The $i$‑th line $(1 \le i \le K)$ should contain the answer for $k = i$.

翻译中…

样例

Input #1
1 2
AC
Output #1
0
0
For $k = 1$, the initial string is `AC`; For $k = 2$, it is `ACAC`. Both are already AGC words, so choosing, say, $(l, r) = (1, 1)$ yields $0$ swaps.
Input #2
5 1
CACAAACCCA
Output #2
1
For $k = 1$, the string is `CACAAACCCA`. By following the steps below, the sequence needs only one swap: 1. Choose $(l, r) = (1, 4)$ and reverse the $1$-st through $4$-th characters. The string is now `ACACAACCCA`. 2. Swap the $9$-th and $10$-th characters. The string is now `ACACAACCAC`, which is an AGC word.
Input #3
10 10
ACCCAAAACCCACCACAACA
Output #3
1
2
3
3
3
3
3
3
3
3
Input #4
50 10
ACCCCACAACAAAACCAACCCCACCAACACCAAAACACACAAAACCCCCACCAACACAACAACCCCAACAAACCCAACACACCCACACCAAACCCAAACA
Output #4
10
17
20
22
23
24
25
26
26
26
Input #5
72 10
CCCCACAAAAACCCACACCAAACCACCCCCAAAAACACCACCCCCAAAAAAACAAAAAACCCCCCACCCAAACAACACCACCACAAACCCAACCAACAACCCAAAACAACCCCACCACACACCACACCCACAAACACAACAACA
Output #5
28
42
51
54
57
60
63
64
65
66
题目信息
题号
agc072_b
时间限制
2 s
内存限制
1024 MB

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