CF1641B.Repetitions Decoding
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Olya has an array of integers a1,a2,…,an. She wants to split it into tandem repeats. Since it's rarely possible, before that she wants to perform the following operation several (possibly, zero) number of times: insert a pair of equal numbers into an arbitrary position. Help her!
More formally:
- A tandem repeat is a sequence x of even length 2k such that for each 1≤i≤k the condition xi=xi+k is satisfied.
- An array a could be split into tandem repeats if you can split it into several parts, each being a subsegment of the array, such that each part is a tandem repeat.
- In one operation you can choose an arbitrary letter c and insert [c,c] to any position in the array (at the beginning, between any two integers, or at the end).
- You are to perform several operations and split the array into tandem repeats or determine that it is impossible. Please note that you do not have to minimize the number of operations.
奥莉娅有一个整数数组 a1,a2,…,an。她希望将该数组分割为若干个并列重复段(tandem repeat)。由于这通常无法直接实现,她决定先执行若干次(可能为零次)如下操作:在数组的任意位置插入一对相等的数。请你帮助她完成这一任务!
更形式化地定义如下:
- 一个并列重复段是指长度为偶数 2k 的序列 x,满足对每个 1≤i≤k,均有 xi=xi+k。
- 若能将数组 a 划分为若干个连续子段(即子数组),且每个子段均为一个并列重复段,则称该数组可被划分为并列重复段。
- 每次操作中,你可以任选一个整数 c,并将 [c,c] 插入到数组中的任意位置(开头、任意两个元素之间,或末尾)。
- 你需要执行若干次上述操作,使得最终数组可被划分为并列重复段;若不可能,请判定其不可行。注意:你无需最小化操作次数。
输入格式
Each test contains multiple test cases. The first line contains a single integer t (1≤t≤30000) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤500).
The second line contains n integers a1,a2,…,an (1≤ai≤109) — the initial array.
It is guaranteed that the sum of n2 over all test cases does not exceed 250000.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤30000),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤500)。
第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109),表示初始数组。
保证所有测试用例中 n2 的总和不超过 250000。
输出格式
For each test case print answer in the following format.
If you cannot turn the array into a concatenation of tandem repeats, print a single integer −1.
Otherwise print the number of operations q (0≤q≤2⋅n2) that you want to do. Then print the descriptions of operations.
In each of the following q lines print two integers p and c (1≤c≤109), which mean that you insert the integer c twice after p elements of the array. If the length of the array is m before the operation, then the condition 0≤p≤m should be satisfied.
Then you should print any way to split the resulting array into tandem repeats. First, print a single integer d, and then print a sequence t1,t2,…,td of even integers of size d (d,ti≥1). These numbers are the lengths of the subsegments from left to right.
Note that the size of the resulting array a is m=n+2⋅q. The following statements must hold:
- m=i=1∑dti.
- For all integer i such that 1≤i≤d, the sequence al,al+1,…,ar is a tandem repeat, where l=j=1∑i−1tj+1, r=l+ti−1.
It can be shown that if the array can be turned into a concatenation of tandem repeats, then there exists a solution satisfying all constraints. If there are multiple answers, you can print any.
对于每个测试用例,请按以下格式输出答案:
- 如果无法将数组变为若干个并置重复串(tandem repeat)的拼接,则输出单个整数 −1;
- 否则,首先输出你希望执行的操作次数 q(满足 0≤q≤2⋅n2),然后输出 q 行操作描述。
在接下来的 q 行中,每行输出两个整数 p 和 c(其中 1≤c≤109),表示在当前数组的前 p 个元素之后插入整数 c 两次。若该操作执行前数组长度为 m,则需满足 0≤p≤m。
随后,你需要输出一种将最终得到的数组划分为若干并置重复串的方式:首先输出一个整数 d,再输出一个长度为 d 的偶数序列 t1,t2,…,td(其中 d,ti≥1)。这些数表示从左到右各子段的长度。
注意:最终数组 a 的长度为 m=n+2⋅q,且必须满足以下条件:
- m=i=1∑dti;
- 对任意整数 i 满足 1≤i≤d,记 l=j=1∑i−1tj+1、r=l+ti−1,则子序列 al,al+1,…,ar 是一个并置重复串。
可以证明:若原数组能被转化为若干并置重复串的拼接,则必存在满足所有约束条件的解。若存在多个合法答案,输出任意一个即可。
输入输出样例
输入#1
4 2 5 7 2 5 5 6 1 3 1 2 2 3 6 3 2 1 1 2 3
输出#1
-1 0 1 2 4 1 3 5 3 5 3 10 3 2 8 6 5 0 3 8 3 5 3 6 2 7 1 4 2 6 6 2
说明/提示
In the first test case, you cannot apply operations to the array to make it possible to split it into tandem repeats.
In the second test case the array is already a tandem repeat [5,5]=t1=2([5]+[5]), thus we can do no operations at all.
In the third test case, initially, we have the following array: $$[1, 3, 1, 2, 2, 3].$$ After the first insertion with p=1,c=3: $$[1, \textbf{3, 3}, 3, 1, 2, 2, 3].$$ After the second insertion with p=5,c=3: $$[1, 3, 3, 3, 1, \textbf{3, 3}, 2, 2, 3].$$ After the third insertion with p=5,c=3: $$[1, 3, 3, 3, 1, \textbf{3, 3}, 3, 3, 2, 2, 3].$$ After the fourth insertion with p=10,c=3: $$[1, 3, 3, 3, 1, 3, 3, 3, 3, 2, \textbf{3, 3}, 2, 3].$$ The resulting array can be represented as a concatenation of tandem repeats: $$\underbrace{([1, 3, 3, 3] + [1, 3, 3, 3])}_{t_1 = 8} + \underbrace{([3, 2, 3] + [3, 2, 3])}_{t_2 = 6}.$$
In the fourth test case, initially, we have the following array: $$[3, 2, 1, 1, 2, 3].$$ After the first insertion with p=0,c=3: $$[\textbf{3, 3}, 3, 2, 1, 1, 2, 3].$$ After the second insertion with p=8,c=3: $$[3, 3, 3, 2, 1, 1, 2, 3, \textbf{3, 3}].$$ After the third insertion with p=5,c=3 $$[3, 3, 3, 2, 1, \textbf{3, 3}, 1, 2, 3, 3, 3].$$ After the fourth insertion with p=6,c=2: $$[3, 3, 3, 2, 1, 3, \textbf{2, 2}, 3, 1, 2, 3, 3, 3].$$ After the fifth insertion with p=7,c=1: $$[3, 3, 3, 2, 1, 3, 2, \textbf{1, 1}, 2, 3, 1, 2, 3, 3, 3].$$ The resulting array can be represented as a concatenation of tandem repeats: $$\underbrace{([3] + [3])}_{t_1 = 2} + \underbrace{([3, 2, 1] + [3, 2, 1])}_{t_2 = 6} + \underbrace{([1, 2, 3] + [1, 2, 3])}_{t_3 = 6} + \underbrace{([3] + [3])}_{t_4 = 2}.$$
在第一个测试用例中,你无法对数组执行任何操作,使其能够被拆分为若干个并系重复(tandem repeat)。
在第二个测试用例中,该数组本身已是一个并系重复 [5,5]=t1=2([5]+[5]),因此我们完全无需执行任何操作。
在第三个测试用例中,初始数组为:
[1,3,1,2,2,3].
第一次插入(p=1,c=3)后:
[1,3,3,3,1,2,2,3].
第二次插入(p=5,c=3)后:
[1,3,3,3,1,3,3,2,2,3].
第三次插入(p=5,c=3)后:
[1,3,3,3,1,3,3,3,3,2,2,3].
第四次插入(p=10,c=3)后:
[1,3,3,3,1,3,3,3,3,2,3,3,2,3].
最终得到的数组可表示为若干并系重复的拼接:
t1=8([1,3,3,3]+[1,3,3,3])+t2=6([3,2,3]+[3,2,3]).
在第四个测试用例中,初始数组为:
[3,2,1,1,2,3].
第一次插入(p=0,c=3)后:
[3,3,3,2,1,1,2,3].
第二次插入(p=8,c=3)后:
[3,3,3,2,1,1,2,3,3,3].
第三次插入(p=5,c=3)后:
[3,3,3,2,1,3,3,1,2,3,3,3].
第四次插入(p=6,c=2)后:
[3,3,3,2,1,3,2,2,3,1,2,3,3,3].
第五次插入(p=7,c=1)后:
[3,3,3,2,1,3,2,1,1,2,3,1,2,3,3,3].
最终得到的数组可表示为若干并系重复的拼接:
t1=2([3]+[3])+t2=6([3,2,1]+[3,2,1])+t3=6([1,2,3]+[1,2,3])+t4=2([3]+[3]).
输入解题思路,AI测评打分。不知道怎么写?