Codeforces Round #638 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 (1t501≤t≤50 ) — the number of test cases.

The first line of each test case contains two integers nn and kk (1kn1001≤k≤n≤100 ).

The second line of each test case contains nn space-separated integers (1ain1≤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 (nm104n≤m≤104 ). You don't need to minimize mm .

The second line should contain mm space-separated integers (1bin1≤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
Copy
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
Copy
5
1 2 1 2 1
4
1 2 2 1
-1
7
4 3 2 1 4 3 2
大意就是往一个序列里插入一些数(1~n),使得插入后的序列每连续k个数的和一样。
注意到很重要的一句话:You don't need to minimize m. 这意味着怎么瞎邒构造都行。首先考虑不可能的情况:当原序列有大于k个不同的数时必然是不可能的,子序列的和必然不可能完全相同(>k个数没法塞到一个子序列里)。
我想的一种构造方法是给原序列去重,依次输出,如果set的大小小于k的话则随便输出k-s.size()个1到n的数补齐,这样的操作只要重复n次即可保证原序列的数都包含在里面。范围很小,懒得想更优雅的方法了。
#include <bits/stdc++.h>
using namespace std;
int n,k;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        vector<int>v;
        set<int>s;
        cin>>n>>k;
        int i;
        for(i=1;i<=n;i++)
        {
            int temp;
            scanf("%d",&temp);
            v.push_back(temp);
            s.insert(temp);
        }
        if(s.size()>k) cout<<-1<<endl;
        else
        {
            cout<<k*n<<endl;
            for(i=1;i<=n;i++)
            {
                set<int>::iterator it;
                for(it=s.begin();it!=s.end();it++) cout<<*it<<' ';
                if(s.size()<k)
                {
                    int j;
                    for(j=1;j<=k-s.size();j++) cout<<1<<' '; 
                }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lipoicyclic/p/12818112.html