CodeForces - 1348B Phoenix and Beauty(思维+题干细节)

题目链接https://vjudge.net/contest/371907#problem/B
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 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 aa. 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 aa beautiful, we can always make it with resulting length no more than 104
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的数组,1<=ai<=n。问能否添加一些范围在1~n之间的数,使得数组长度为k的子序列的和都相等。否则输出-1。
例如:
4 2
1 2 2 1
变成
5
1 2 1 2 1
长度为2的子序列的和都为3

分析
要使一个数组对于某些k来说是漂亮的,这个数组必须以k为周期。
如果数组a中存在多于k个不同的数字,则没有答案,我们输出-1(因为数组不能以k为周期)。
如果数组a中不同的数字的数量小于k我们将添加一些数字,直到不同数字的数量为k。我们可以把这个列表打印n次。我们的数组b的长度是n*k。
n和k的取值范围为1≤k≤n≤100,最大不超过104

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e2+10;
const int M=1e4+10;
int a[N],b[N],index;
bool book[N];
void init()
{
    index=0;
    memset(b,0,sizeof(b));
    memset(book,false,sizeof(book));
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int n,k;
        scanf("%d%d",&n,&k);
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        int length=unique(a,a+n)-a;
        if(length>k)
        {
            printf("-1\n");
            continue;
        }
        for(int i=0; i<length; i++)
        {
            b[index++]=a[i];
            book[a[i]]=true;
        }
        if(index<k)
        {
            for(int i=1; i<=n; i++)
            {
                if(!book[i])
                {
                    book[i]=true;
                    b[index++]=i;
                    if(index==k)
                        break;
                }
            }
        }
        printf("%d\n",n*k);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<index; j++)
                printf("%d ",b[j]);
        }
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值