B - Phoenix and Beauty
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 2
Output
5
1 2 1 2 1
4
1 2 2 1
-1
7
4 3 2 1 4 3 2
题意
给出n个数任意连续k个数和相等,数小于n,可以插入数字。
思路
如果出现的数字的种类小于k可构建一个周期为k的数串。
代码
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int t,k,n,i,j;
cin>>t;
while(t--){
cin>>n>>k;
int a[101],b[101]={0},c[101],sum;//再循环内创建如果在外面前一个样例最大值大于第二个会因为数组内部出问题。
for(i=1;i<=n;i++){ //统计那些数字出现种类数。
cin>>a[i];
b[a[i]]=1;
}
int count=0;
for(i=0;i<=100;i++){ //种类数。
if(b[i])count++;
}
if(count>k){ //无法构成k循环
cout<<"-1"<<endl;
continue;
}
if(count<k){
for(i=1;i<=100;i++){ //补充数字确定循环
if(b[i]==0){
b[i]=1;
count++;
}
if(count==k)break;
}
}
sum=0;
for(i=1;i<=100;i++){
if(b[i])c[++sum]=i;
}
cout<<n*k<<endl;
for(i=1;i<=n;i++){ //每一个数字输出一次循环
for(j=1;j<=k;j++){
cout<<c[j]<<" ";
}
}
cout<<endl;
}
}