輸入
4
4 2
1 2 2 1
4 3
1 2 2 1
3 2
1 2 3
4 4
4 3 4 2
輸出
5
1 2 1 2 1
4
1 2 2 1
-1
7
4 3 2 1 4 3 2
題意:給定序列長n的數組和k。完美數組的定義是數組中每一個連續k項的子段和爲定值,現在要求插入一些數使得數組滿足條件,輸出你構造的新數列.
當數組中數的種類大於k是一定不能構造的,那我們現在考慮小於k的情況。(下面的數組a是假定構造完的數列)
定值爲a[1]到a[k]的子段和,說明a[2]到a[k+1]也等於這個定值,所以a[k+1]==a[1]
那麼現在很簡單,我們只需要a[1]到a[k]就可以一直往後構造(一個循環節)
a[1]到a[k]用原數組中互不相同的數字填充,不夠的話你隨便添加什麼都可以啦 然後就一直構造n次…
代碼:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+100;
const int inf=0x3f3f3f3f;
int a[maxn],vis[maxn],c[maxn];
int main()
{
int t;
cin>>t;
while(t--)
{
memset(a,0,sizeof a);
memset(vis,0,sizeof vis);
memset(c,0,sizeof c);
int n,k,num=0;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(vis[a[i]]==0)
{
vis[a[i]]=1;
c[++num]=a[i];
}
}
if(num>k){
cout<<-1<<endl;
}
else
{
for(int i=num+1;i<=k;i++)
c[i]=c[i-1];
for(int i=k+1;i<=9999;i++)
c[i]=c[i-k];
cout<<9999<<endl;
for(int i=1;i<=9999;i++)
cout<<c[i]<<" ";
cout<<endl;
}
}
return 0;
}
在cf提交時候結尾加上system(“pause”);好像過不了