#638 (Div. 2)B. Phoenix and Beauty(思维)

Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length k have the same sum. A subarray of an array is any sequence of consecutive elements.
Phoenix currently has an array a of length n. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between 1 and n 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 t (1≤t≤50) — the number of test cases.
The first line of each test case contains two integers n and k (1≤k≤n≤100).
The second line of each test case contains n space-separated integers (1≤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 m (n≤m≤104). You don’t need to minimize m.
The second line should contain m space-separated integers (1≤bi≤n) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array a. You may print integers that weren’t originally in array a.
If there are multiple solutions, print any. It’s guaranteed that if we can make array a beautiful, we can always make it with resulting length no more than 104.

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

Note

In the first test case, we can make array a beautiful by inserting the integer 1 at index 3 (in between the two existing 2s). Now, all subarrays of length k=2 have the same sum 3. There exists many other possible solutions, for example:
2,1,2,1,2,1
1,2,1,2,1,2
In the second test case, the array is already beautiful: all subarrays of length k=3 have the same sum 5.
In the third test case, it can be shown that we cannot insert numbers to make array a beautiful.
In the fourth test case, the array b shown is beautiful and all subarrays of length k=4 have the same sum 10. There exist other solutions also.

题目大意:
给出一个长度为n的序列,你可以在序列的任意位置插入任意的一个1-n之间的数(不需要使得序列长度最小),保证修改后的序列小于m。使得这个序列的任意连续k个数的和都相等。

这个我想错了好几次,卡了我一个多小时才搞出来的。差点以为我做不出来了。
题目分析:
这个题主要是其中的规律不太好找,只要找到了规律,就很简单了。

1)首先我们可以发现:n和k的数据范围是100,而m的数据范围是1-1e4。(记住这个数据范围!)
2)然后就是找规律了,怎么才构造序列才能使得序列满足条件。
我们可以发现:当该序列中的数的种类小于等于k时,该序列一定可以修改为满足条件的序列。反之则一定不能修改为满足条件的数。
3)下一步就是构造新序列了。
我们可以找出一段序列,这段序列有k种数(每种数都只有1个,且属于1-n,注意:这段序列中必定包含原序列的所以数),然后我们可以用这段序列来替换每一个原序列中的一个数。这样就能得到答案了
可能有点乱,我们以这样例为例:
4 4
4 3 4 2
这段序列有三种数,cnt=3<4.所以该序列还要再加上1个数得到序列2,3,4,1.
因此,得到的答案序列为
16
2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1.

这样得到的序列长的为nk,而nk<=m,所以符合条件。

ac代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=1e4+5;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		int a[N]={0};     //用a[]来记录种类数
		cin>>n>>k;
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			a[x]++;
		}
		int cnt=0;            //遍历a[]得到种类数cnt
		for(int i=1;i<=n;i++)
		{
			if(a[i]) cnt++;
		}
		if(cnt>k) {puts("-1"); continue;}  //cnt>k,不符合条件
		int b[N]={0};         //b储存替换序列
		for(int i=1,t=0;i<=n;i++)
		{
			if(a[i]) b[++t]=i;
		}
		if(cnt<k)            //看cnt是否等于k,如果不等于,还要往里加数。
		{
			for(int i=1;cnt!=k;i++)
			if(!a[i]) b[++cnt]=i;
		}
		cout<<n*k<<endl;        //将原序列的每一个数替换成该段序列输出
		for(int i=1;i<=n;i++)
		for(int j=1;j<=cnt;j++)
		cout<<b[j]<<' ';
		cout<<endl;
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/105886444