CodeForces - 1348B
Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length kk have the same sum. A subarray of an array is any sequence of consecutive elements.
Phoenix currently has an array aa of length nn. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between 11 and nn inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.
Input
The input consists of multiple test cases. The first line contains an integer tt (1≤t≤501≤t≤50) — the number of test cases.
The first line of each test case contains two integers nn and kk (1≤k≤n≤1001≤k≤n≤100).
The second line of each test case contains nn space-separated integers (1≤ai≤n1≤ai≤n) — the array that Phoenix currently has. This array may or may not be already beautiful.
Output
For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines.
The first line should contain the length of the beautiful array mm (n≤m≤104n≤m≤104). You don't need to minimize mm.
The second line should contain mm space-separated integers (1≤bi≤n1≤bi≤n) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array aa. You may print integers that weren't originally in array aa.
If there are multiple solutions, print any. It's guaranteed that if we can make array aa beautiful, we can always make it with resulting length no more than 104104.
Example
Input
4 4 2 1 2 2 1 4 3 1 2 2 1 3 2 1 2 3 4 4 4 3 4 2Output
5 1 2 1 2 1 4 1 2 2 1 -1 7 4 3 2 1 4 3 2Note
In the first test case, we can make array aa beautiful by inserting the integer 11 at index 33 (in between the two existing 22s). Now, all subarrays of length k=2k=2 have the same sum 33. There exists many other possible solutions, for example:
2,1,2,1,2,12,1,2,1,2,11,2,1,2,1,21,2,1,2,1,2In the second test case, the array is already beautiful: all subarrays of length k=3k=3 have the same sum 55.
In the third test case, it can be shown that we cannot insert numbers to make array aa beautiful.
In the fourth test case, the array bb shown is beautiful and all subarrays of length k=4k=4 have the same sum 1010. There exist other solutions also.
题意:t组数据,每组给定数组长度n和子序列长度k,以及n给元素组成数组。要求在数组中(任意位置)插入元素,使得构造的新数组的连续的k长度的子序列之和都相等。
题解:既然子序列长度为k,如果我数组中出现的不同数字个数超过了k,那么我无论如何都无法构造满足条件的新数组的。然后就是怎么构造数组了,(这是我最笨的方法)直接绑定数组里的第一个元素,然后往后k-1个元素全部通过插入获得,当然插入的数必须是数组中原来就包含的,如果不同数字插完了还未满长度k,就插入第一个元素直到长度为k。然后遍历元素组,将原数组的元素插入构造新数组的末尾,要求即b[i] = b[i-k]。表达的有点奇怪,看代码吧。
#include<iostream> #include<algorithm> using namespace std; int t, n, k, a[10005]; int main(){ scanf("%d", &t); while(t--){ int flag[105]={0}, jud[105]={0}, cnt=0; int b[10005]; scanf("%d %d", &n, &k); for(int i=0; i<n; i++){ scanf("%d", a+i); flag[a[i]]++;//标记数字a[i]出现过 } for(int i=1; i<=n; i++) if(flag[i]) cnt++;//统计出现过多少个不同数字 if(cnt>k){printf("-1\n");continue;} int tmp = 1; b[0] = a[0]; flag[a[0]] = 0; for(int i=1; i<=n&&tmp<k; i++){ if(flag[i]){ flag[i] = 0; b[tmp++] = i; } } for(; tmp<k; tmp++) b[tmp] = b[0]; for(int i=1; i<n; i++){ while(a[i]!=b[tmp-k]){ b[tmp] = b[tmp-k]; tmp++; } b[tmp] = b[tmp-k]; tmp++; } while(tmp%k){ b[tmp] = b[tmp-k]; tmp++; } printf("%d\n", tmp); for(int i=0; i<tmp; i++) printf("%d ", b[i]); printf("\n"); } }