CF2163B.Siga ta Kymata
普及+/提高
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation∗ p of every integer from 1 to n. You also own a binary† string s of size n where si=0 for all 1≤i≤n. You may do the following operation at most 5 times:
- Choose any two integers l and r such that 1≤l≤r≤n. Then, for every i such that l<i<r and min(pl,pr)<pi<max(pl,pr) hold at the same time, you will set si to 1.
You are also given a binary string x of size n. After performing operations, it must hold for every 1≤i≤n that if xi=1, then si=1. Note that if xi=0, then si can have any value.
Figure out any sequence of at most 5 operations such that the aforementioned condition is satisfied, or report that it is impossible to do so. Note that you do not have to minimize the number of operations you make.
∗A permutation p of every integer from 1 to n is a sequence of elements from 1 to n such that every element appears exactly once.
†A string b of size m is considered binary if and only if bi=0 or bi=1 for all 1≤i≤m.
你被给定一个 1 到 n 的排列∗ p。你还拥有一个长度为 n 的二进制† 字符串 s,其中对所有 1≤i≤n,均有 si=0。你最多可执行以下操作 5 次:
- 任选两个整数 l 和 r,满足 1≤l≤r≤n。然后,对每个满足 l<i<r 且 min(pl,pr)<pi<max(pl,pr) 的下标 i,将 si 设为 1。
你还会被给定一个长度为 n 的二进制字符串 x。执行若干次操作后,必须满足:对每个 1≤i≤n,若 xi=1,则 si=1。注意,若 xi=0,则 si 可为任意值(0 或 1)。
请找出任意一个至多包含 5 次操作的操作序列,使得上述条件成立;若不存在这样的操作序列,则报告其不可能。注意,你无需最小化所用操作次数。
∗ 一个 1 到 n 的排列 p 是由 1 到 n 中的元素构成的序列,其中每个元素恰好出现一次。
† 一个长度为 m 的字符串 b 被称为二进制字符串,当且仅当对所有 1≤i≤m,均有 bi=0 或 bi=1。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (3≤n≤2⋅105) — the size of the array.
The second line contains exactly n integers p1,p2,…,pn (1≤pi≤n, the elements of p are pairwise distinct) — where pi is the i-th element of the permutation.
The third line contains a single binary string x of size n.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(3≤n≤2⋅105)—— 数组的大小。
第二行包含恰好 n 个整数 p1,p2,…,pn(1≤pi≤n,且 p 中的元素两两不同)—— 其中 pi 表示该排列的第 i 个元素。
第三行包含一个长度为 n 的二进制字符串 x。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, if it is impossible to perform operations such that the constraint is satisfied, output −1.
Otherwise, output an integer 0≤k≤5, the number of operations. On the i-th of the next k lines, output two integers 1≤li≤ri≤n, the bounds of the i-th operation that is performed. If there are multiple correct solutions, output any of them.
对于每个测试用例,如果无法通过执行操作来满足约束条件,则输出 −1。
否则,输出一个整数 0≤k≤5,表示执行的操作次数。接下来的 k 行中,第 i 行输出两个整数 1≤li≤ri≤n,表示第 i 次操作所作用的区间边界。若存在多种正确解,输出任意一种即可。
输入输出样例
输入#1
6 3 1 2 3 010 5 3 4 2 1 5 11111 6 1 3 2 4 6 5 001100 6 6 2 3 4 5 1 110110 5 2 1 4 3 5 00000 5 2 5 3 1 4 00100
输出#1
1 1 3 -1 2 1 5 2 6 -1 0 1 2 4
说明/提示
In the first example, p=[1,2,3], and x=010. We can perform a single operation, with l=1 and r=3. After the operation, we set s2 to 1 since l<2<r and min(pl,pr)<p2=2<max(pl,pr) hold at the same time. As a result, s=010.
In the second example, it can be shown that there does not exist a correct sequence of at most 5 operations, so we output −1.
In the third example, p=[1,3,2,4,6,5] and x=001100. After performing an operation for l=1 and r=5, then s=011100. If we also perform an operation for l=2 and r=6, then s will remain the same. The string s=011100 is valid, because for every position where x has a 1, then s also has a 1 at that position.
在第一个例子中,p=[1,2,3],且 x=010。我们可以执行一次操作,取 l=1 且 r=3。操作后,我们将 s2 设为 1,因为同时满足 l<2<r 以及 min(pl,pr)<p2=2<max(pl,pr)。最终得到 s=010。
在第二个例子中,可以证明不存在长度不超过 5 的合法操作序列,因此输出 −1。
在第三个例子中,p=[1,3,2,4,6,5] 且 x=001100。先对 l=1 和 r=5 执行一次操作,则 s=011100。若再对 l=2 和 r=6 执行一次操作,则 s 保持不变。字符串 s=011100 是合法的,因为对于 x 中每个值为 1 的位置,s 在该位置上也均为 1。
输入解题思路,AI测评打分。不知道怎么写?