CF1736D.Equal Binary Subsequences

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Everool has a binary string ss of length 2n2n. Note that a binary string is a string consisting of only characters 00 and 11. He wants to partition ss into two disjoint equal subsequences. He needs your help to do it.

You are allowed to do the following operation exactly once.

  • You can choose any subsequence (possibly empty) of ss and rotate it right by one position.

In other words, you can select a sequence of indices b1,b2,…,bmb_1, b_2, \ldots, b_m, where 1≤b1<b2<…<bm≤2n1 \le b_1 \lt b_2 \lt \ldots \lt b_m \le 2n. After that you simultaneously set $$s_{b_1} := s_{b_m},$$ $$s_{b_2} := s_{b_1},$$ $$\ldots,$$ $$s_{b_m} := s_{b_{m-1}}.$$

Can you partition ss into two disjoint equal subsequences after performing the allowed operation exactly once?

A partition of ss into two disjoint equal subsequences sps^p and sqs^q is two increasing arrays of indices p1,p2,…,pnp_1, p_2, \ldots, p_n and q1,q2,…,qnq_1, q_2, \ldots, q_n, such that each integer from 11 to 2n2n is encountered in either pp or qq exactly once, sp=sp1sp2…spns^p = s_{p_1} s_{p_2} \ldots s_{p_n}, sq=sq1sq2…sqns^q = s_{q_1} s_{q_2} \ldots s_{q_n}, and sp=sqs^p = s^q.

If it is not possible to partition after performing any kind of operation, report −1-1.

If it is possible to do the operation and partition ss into two disjoint subsequences sps^p and sqs^q, such that sp=sqs^p = s^q, print elements of bb and indices of sps^p, i. e. the values p1,p2,…,pnp_1, p_2, \ldots, p_n.

Everool 有一个长度为 2n2n 的二进制字符串 ss。注意,二进制字符串是指仅由字符 00 和 11 组成的字符串。他希望将 ss 划分为两个互不相交且相等的子序列。他需要你帮助完成这一任务。

你被允许恰好执行一次如下操作:

  • 你可以任选 ss 的一个子序列(可以为空),并将其向右循环移动一位。

换言之,你可以选择一组下标 b1,b2,…,bmb_1, b_2, \ldots, b_m,满足 1≤b1<b2<…<bm≤2n1 \le b_1 \lt b_2 \lt \ldots \lt b_m \le 2n。然后同时执行以下赋值:

sb1:=sbm,s_{b_1} := s_{b_m},

sb2:=sb1,s_{b_2} := s_{b_1},

…,\ldots,

sbm:=sbm−1.s_{b_m} := s_{b_{m-1}}.

在恰好执行一次上述允许的操作后,你能否将 ss 划分为两个互不相交且相等的子序列?

字符串 ss 的一种划分为两个互不相交且相等的子序列 sps^p 和 sqs^q,是指两组严格递增的下标数组 p1,p2,…,pnp_1, p_2, \ldots, p_n 和 q1,q2,…,qnq_1, q_2, \ldots, q_n,使得 11 到 2n2n 中的每个整数在 pp 或 qq 中恰好出现一次,且满足:

  • sp=sp1sp2…spns^p = s_{p_1} s_{p_2} \ldots s_{p_n},
  • sq=sq1sq2…sqns^q = s_{q_1} s_{q_2} \ldots s_{q_n},
  • sp=sqs^p = s^q。

如果无论执行何种操作都无法实现划分,则输出 −1-1。

如果存在某种操作(即选定子序列 bb)及对应划分,使得 ss 可被划分为两个互不相交的子序列 sps^p 和 sqs^q,且满足 sp=sqs^p = s^q,则请输出子序列 bb 的元素以及子序列 sps^p 的下标,即输出 p1,p2,…,pnp_1, p_2, \ldots, p_n。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5), where 2n2n is the length of the binary string.

The second line of each test case contains the binary string ss of length 2n2n.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1051 \le t \le 10^5)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5),其中 2n2n 是二进制字符串的长度。

每个测试用例的第二行包含长度为 2n2n 的二进制字符串 ss。

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

输出格式

For each test case, follow the following output format.

If there is no solution, print −1-1.

Otherwise,

  • In the first line, print an integer mm (0≤m≤2n0 \leq m \leq 2n), followed by mm distinct indices b1b_1, b2b_2, ..., bmb_m(in increasing order).
  • In the second line, print nn distinct indices p1p_1, p2p_2, ..., pnp_n (in increasing order).

If there are multiple solutions, print any.

对于每个测试用例,请按照以下输出格式输出。

若无解,则输出 −1-1。

否则,

  • 在第一行中,先输出一个整数 mm(0≤m≤2n0 \leq m \leq 2n),随后输出 mm 个互不相同的下标 b1b_1, b2b_2, ..., bmb_m(按升序排列);
  • 在第二行中,输出 nn 个互不相同的下标 p1p_1, p2p_2, ..., pnp_n(按升序排列)。

若存在多个解,输出任意一个即可。

输入输出样例

  • 输入#1

    4
    2
    1010
    3
    100010
    2
    1111
    2
    1110

    输出#1

    0
    1 2
    2 3 5
    1 2 5
    3 2 3 4
    1 4
    -1

说明/提示

In the first test case, bb is empty. So string ss is not changed. Now sp=s1s2=10s^p = s_1 s_2 = \mathtt{10}, and sq=s3s4=10s^q = s_3s_4 = \mathtt{10}.

In the second test case, b=[3,5]b=[3,5]. Initially s3=0s_3=\mathtt{0}, and s5=1s_5=\mathtt{1}. On performing the operation, we simultaneously set s3=1s_3=\mathtt{1}, and s5=0s_5=\mathtt{0}.

So ss is updated to 101000 on performing the operation.

Now if we take characters at indices [1,2,5][1,2,5] in sps^p, we get s1=100s_1=\mathtt{100}. Also characters at indices [3,4,6][3,4,6] are in sqs^q. Thus sq=100s^q=100. We are done as sp=sqs^p=s^q.

In fourth test case, it can be proved that it is not possible to partition the string after performing any operation.

在第一个测试用例中,bb 为空。因此字符串 ss 不发生改变。此时 sp=s1s2=10s^p = s_1 s_2 = \mathtt{10},且 sq=s3s4=10s^q = s_3s_4 = \mathtt{10}。

在第二个测试用例中,b=[3,5]b=[3,5]。初始时 s3=0s_3=\mathtt{0},s5=1s_5=\mathtt{1}。执行操作后,我们同时将 s3s_3 设为 1\mathtt{1},并将 s5s_5 设为 0\mathtt{0}。

因此,执行该操作后 ss 更新为 101000。

此时,若取 sps^p 中下标为 [1,2,5][1,2,5] 的字符,得到 s1=100s_1=\mathtt{100};而 sqs^q 包含下标为 [3,4,6][3,4,6] 的字符,故 sq=100s^q=100。由于 sp=sqs^p=s^q,任务完成。

在第四个测试用例中,可以证明:无论执行何种操作,均无法对字符串进行划分。

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

首页