第 76 场 AtCoder Grand Contest
B
分组与反转
Split and Reverse
AtCoder
⌀ 3079
原题
← AC →

分组与反转AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given an integer sequence $X=(X_1,X_2,\cdots,X_N)$ of length $N$ consisting of $0$ and $1$.

给定一个长度为 $N$ 的整数序列 $X=(X_1,X_2,\cdots,X_N)$,其中每个元素都是 $0$ 或 $1$。AI (CI翻译)

You can perform the following operation zero or more times:

你可以进行零次或多次以下操作:AI (CI翻译)

  • Classify each element of $X$ into group A or group B. Then, for each group, reverse the order of the elements it contains. More formally, if the elements contained in a group are $X_{i_1},X_{i_2},\cdots,X_{i_s}$ ($i_1<i_2<\cdots<i_s$), then for all $1 \leq j \leq s$, simultaneously replace the value of $X_{i_j}$ with $X_{i_{s+1-j}}$.
  • 将 $X$ 的每个元素分到 A 组或 B 组。然后分别将每组中元素的顺序反转。更形式化地说,若某组包含的元素为 $X_{i_1},X_{i_2},\cdots,X_{i_s}$($i_1<i_2<\cdots<i_s$),则同时对所有 $1 \leq j \leq s$,将 $X_{i_j}$ 的值替换为 $X_{i_{s+1-j}}$。
AI (CI翻译)

Your goal is to make $X$ non-decreasing. Find the minimum number of operations needed to achieve the goal and the way to perform the operations. It can be proved that a solution always exists under the constraints of this problem.

你的目标是使 $X$ 非递减。请找出实现目标所需的最少操作次数,以及具体的操作方案。可以证明,在本题约束下总是存在解。AI (CI翻译)

Solve $T$ cases for one input.

请针对一份输入中的 $T$ 个测试用例求解。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq T \leq 250000$
  • $1 \leq N \leq 250000$
  • $0 \leq X_i \leq 1$
  • The sum of $N$ over the $T$ cases is at most $250000$.
  • All input values are integers.
  • $1 \leq T \leq 250000$
  • $1 \leq N \leq 250000$
  • $0 \leq X_i \leq 1$
  • 所有测试用例的 $N$ 之和不超过 $250000$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

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

输入通过标准输入给出,格式如下:AI (CI翻译)

T
case_1
case_2
\vdots
case_T

T
case_1
case_2
\vdots
case_T
AI (CI翻译)

Each test case is given in the following format:

每个测试用例的格式如下:AI (CI翻译)

N
X_1 X_2 \cdots X_N

N
X_1 X_2 \cdots X_N
AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each case, output in the following format:

对每个测试用例,按以下格式输出:AI (CI翻译)

k
s_1
s_2
\vdots
s_k

k
s_1
s_2
\vdots
s_k
AI (CI翻译)

Here, $k$ is the minimum number of operations needed, and $s_i$ is a string representing the $i$-th operation. $s_i$ is a string of length $N$ consisting of A and B, and the $j$-th character of $s_i$ represents the group to which $X_j$ is assigned in the $i$-th operation. If there are multiple solutions, you may output any of them.

其中,$k$ 是所需的最少操作次数,$s_i$ 是表示第 $i$ 次操作的字符串。$s_i$ 是一个长度为 $N$、由 A 和 B 组成的字符串,其第 $j$ 个字符表示第 $i$ 次操作中 $X_j$ 所属的组。如果有多个解,可以输出任意一个。AI (CI翻译)

样例

Input #1
2
4
0 1 0 1
5
0 0 1 1 1
Output #1
2
AABA
BBBB
0
For the first test case, the goal can be achieved in two operations as follows, which is the minimum number of operations: - First operation: Use $s_1$=`AABA`. Since $(X_1,X_2,X_4)$ are in group `A`, reverse their order, resulting in $(X_1,X_2,X_4)=(1,1,0)$. Similarly, since $(X_3)$ is in group `B`, reverse their order, resulting in $(X_3)=(0)$. Thus, overall we get $X=(1,1,0,0)$. - Second operation: Use $s_2$=`BBBB`. Since $()$ are in group `A`, reverse their order, resulting in $()=()$. Similarly, since $(X_1,X_2,X_3,X_4)$ are in group `B`, reverse their order, resulting in $(X_1,X_2,X_3,X_4)=(0,0,1,1)$. Thus, overall we get $X=(0,0,1,1)$. For the second test case, the goal can be achieved by performing the operation zero times.
题目信息
题号
agc076_b
时间限制
2 s
内存限制
1024 MB

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