CF1736D.Equal Binary Subsequences
提高+/省选-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Everool has a binary string s of length 2n. Note that a binary string is a string consisting of only characters 0 and 1. He wants to partition s 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 s and rotate it right by one position.
In other words, you can select a sequence of indices b1,b2,…,bm, where 1≤b1<b2<…<bm≤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 s into two disjoint equal subsequences after performing the allowed operation exactly once?
A partition of s into two disjoint equal subsequences sp and sq is two increasing arrays of indices p1,p2,…,pn and q1,q2,…,qn, such that each integer from 1 to 2n is encountered in either p or q exactly once, sp=sp1sp2…spn, sq=sq1sq2…sqn, and sp=sq.
If it is not possible to partition after performing any kind of operation, report −1.
If it is possible to do the operation and partition s into two disjoint subsequences sp and sq, such that sp=sq, print elements of b and indices of sp, i. e. the values p1,p2,…,pn.
Everool 有一个长度为 2n 的二进制字符串 s。注意,二进制字符串是指仅由字符 0 和 1 组成的字符串。他希望将 s 划分为两个互不相交且相等的子序列。他需要你帮助完成这一任务。
你被允许恰好执行一次如下操作:
- 你可以任选 s 的一个子序列(可以为空),并将其向右循环移动一位。
换言之,你可以选择一组下标 b1,b2,…,bm,满足 1≤b1<b2<…<bm≤2n。然后同时执行以下赋值:
sb1:=sbm,
sb2:=sb1,
…,
sbm:=sbm−1.
在恰好执行一次上述允许的操作后,你能否将 s 划分为两个互不相交且相等的子序列?
字符串 s 的一种划分为两个互不相交且相等的子序列 sp 和 sq,是指两组严格递增的下标数组 p1,p2,…,pn 和 q1,q2,…,qn,使得 1 到 2n 中的每个整数在 p 或 q 中恰好出现一次,且满足:
- sp=sp1sp2…spn,
- sq=sq1sq2…sqn,
- sp=sq。
如果无论执行何种操作都无法实现划分,则输出 −1。
如果存在某种操作(即选定子序列 b)及对应划分,使得 s 可被划分为两个互不相交的子序列 sp 和 sq,且满足 sp=sq,则请输出子序列 b 的元素以及子序列 sp 的下标,即输出 p1,p2,…,pn。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤105). Description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤105), where 2n is the length of the binary string.
The second line of each test case contains the binary string s of length 2n.
It is guaranteed that the sum of n over all test cases does not exceed 105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤105)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤105),其中 2n 是二进制字符串的长度。
每个测试用例的第二行包含长度为 2n 的二进制字符串 s。
保证所有测试用例的 n 值之和不超过 105。
输出格式
For each test case, follow the following output format.
If there is no solution, print −1.
Otherwise,
- In the first line, print an integer m (0≤m≤2n), followed by m distinct indices b1, b2, ..., bm(in increasing order).
- In the second line, print n distinct indices p1, p2, ..., pn (in increasing order).
If there are multiple solutions, print any.
对于每个测试用例,请按照以下输出格式输出。
若无解,则输出 −1。
否则,
- 在第一行中,先输出一个整数 m(0≤m≤2n),随后输出 m 个互不相同的下标 b1, b2, ..., bm(按升序排列);
- 在第二行中,输出 n 个互不相同的下标 p1, p2, ..., pn(按升序排列)。
若存在多个解,输出任意一个即可。
输入输出样例
输入#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, b is empty. So string s is not changed. Now sp=s1s2=10, and sq=s3s4=10.
In the second test case, b=[3,5]. Initially s3=0, and s5=1. On performing the operation, we simultaneously set s3=1, and s5=0.
So s is updated to 101000 on performing the operation.
Now if we take characters at indices [1,2,5] in sp, we get s1=100. Also characters at indices [3,4,6] are in sq. Thus sq=100. We are done as sp=sq.
In fourth test case, it can be proved that it is not possible to partition the string after performing any operation.
在第一个测试用例中,b 为空。因此字符串 s 不发生改变。此时 sp=s1s2=10,且 sq=s3s4=10。
在第二个测试用例中,b=[3,5]。初始时 s3=0,s5=1。执行操作后,我们同时将 s3 设为 1,并将 s5 设为 0。
因此,执行该操作后 s 更新为 101000。
此时,若取 sp 中下标为 [1,2,5] 的字符,得到 s1=100;而 sq 包含下标为 [3,4,6] 的字符,故 sq=100。由于 sp=sq,任务完成。
在第四个测试用例中,可以证明:无论执行何种操作,均无法对字符串进行划分。
输入解题思路,AI测评打分。不知道怎么写?