CF1605B.Reverse Sort

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Ashish has a binary string ss of length nn that he wants to sort in non-decreasing order.

He can perform the following operation:

  1. Choose a subsequence of any length such that its elements are in non-increasing order. Formally, choose any kk such that 1≤k≤n1 \leq k \leq n and any sequence of kk indices 1≤i1<i2<…<ik≤n1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le n such that si1≥si2≥…≥siks_{i_1} \ge s_{i_2} \ge \ldots \ge s_{i_k}.
  2. Reverse this subsequence in-place. Formally, swap si1s_{i_1} with siks_{i_k}, swap si2s_{i_2} with sik−1s_{i_{k-1}}, …\ldots and swap si⌊k/2⌋s_{i_{\lfloor k/2 \rfloor}} with si⌈k/2⌉+1s_{i_{\lceil k/2 \rceil + 1}} (Here ⌊x⌋\lfloor x \rfloor denotes the largest integer not exceeding xx, and ⌈x⌉\lceil x \rceil denotes the smallest integer not less than xx)

Find the minimum number of operations required to sort the string in non-decreasing order. It can be proven that it is always possible to sort the given binary string in at most nn operations.

阿什什有一个长度为 nn 的二进制字符串 ss,他希望将其按非递减顺序排序。

他可以执行以下操作:

  1. 选择一个任意长度的子序列,使得该子序列中的元素呈非递增顺序。形式化地,选择任意满足 1≤k≤n1 \leq k \leq n 的整数 kk,以及任意满足 1≤i1<i2<…<ik≤n1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le n 的 kk 个下标,使得 si1≥si2≥…≥siks_{i_1} \ge s_{i_2} \ge \ldots \ge s_{i_k}。
  2. 就地反转该子序列。形式化地,交换 si1s_{i_1} 与 siks_{i_k},交换 si2s_{i_2} 与 sik−1s_{i_{k-1}},……,并交换 si⌊k/2⌋s_{i_{\lfloor k/2 \rfloor}} 与 si⌈k/2⌉+1s_{i_{\lceil k/2 \rceil + 1}}(其中 ⌊x⌋\lfloor x \rfloor 表示不超过 xx 的最大整数,⌈x⌉\lceil x \rceil 表示不小于 xx 的最小整数)。

求将该字符串排序为非递减顺序所需的最少操作次数。可以证明:对任意给定的二进制字符串,总能在至多 nn 次操作内完成排序。

输入格式

The first line contains a single integer tt (1≤t≤1000)(1 \le t \le 1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤1000)(1 \le n \le 1000) — the length of the binary string ss.

The second line of each test case contains a binary string ss of length nn containing only 00s and 11s.

It is guaranteed that the sum of nn over all test cases does not exceed 10001000.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤10001 \le n \le 1000)—— 表示二进制字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss,其中仅包含字符 00 和 11。

保证所有测试用例的 nn 值之和不超过 10001000。

输出格式

For each test case output the following:

  • The minimum number of operations mm in the first line (0≤m≤n0 \le m \le n).
  • Each of the following mm lines should be of the form: kk i1i_1 i2i_2 ... iki_{k}, where kk is the length and i1<i2<...<iki_1 \lt i_2 \lt ... \lt i_{k} are the indices of the chosen subsequence. For them the conditions from the statement must hold.

对每个测试用例,输出以下内容:

  • 第一行输出操作的最小次数 mm(0≤m≤n0 \le m \le n)。
  • 接下来的 mm 行中,每行格式为:kk i1i_1 i2i_2 ... iki_{k},其中 kk 为子序列长度,i1<i2<...<iki_1 \lt i_2 \lt ... \lt i_{k} 为所选子序列的下标。这些下标需满足题目陈述中的条件。

输入输出样例

  • 输入#1

    3
    7
    0011111
    5
    10100
    6
    001000

    输出#1

    0
    1
    4 1 3 4 5 
    1
    3 3 5 6

说明/提示

In the first test case, the binary string is already sorted in non-decreasing order.

In the second test case, we can perform the following operation:

  • k=4:k = 4: choose the indices 1,3,4,5{1, 3, 4, 5}

    1‾\underline{1} 00 1‾\underline{1} 0‾\underline{0} 0‾\underline{0} $\rightarrow $ 0‾\underline{0} 00 0‾\underline{0} 1‾\underline{1} 1‾\underline{1}

In the third test case, we can perform the following operation:

  • k=3:k = 3: choose the indices 3,5,6{3, 5, 6}

    00 00 1‾\underline{1} 00 0‾\underline{0} 0‾\underline{0} $\rightarrow $ 00 00 0‾\underline{0} 00 0‾\underline{0} 1‾\underline{1}

在第一个测试用例中,该二进制字符串本身已按非递减顺序排序。

在第二个测试用例中,我们可以执行以下操作:

  • k=4:k = 4: 选择下标 1,3,4,5{1, 3, 4, 5}

    1‾\underline{1} 00 1‾\underline{1} 0‾\underline{0} 0‾\underline{0} $\rightarrow $ 0‾\underline{0} 00 0‾\underline{0} 1‾\underline{1} 1‾\underline{1}

在第三个测试用例中,我们可以执行以下操作:

  • k=3:k = 3: 选择下标 3,5,6{3, 5, 6}

    00 00 1‾\underline{1} 00 0‾\underline{0} 0‾\underline{0} $\rightarrow $ 00 00 0‾\underline{0} 00 0‾\underline{0} 1‾\underline{1}

输入解题思路,AI测评打分。不知道怎么写?

首页