CodeForces 1348-B Phoenix and Beauty(思维、子序列和相等)

https://codeforces.com/contest/1348/problem/B

 

题意:

给出一个n个数的序列a,可以向这个序列中的任意地方插入1-n中的数,问是否可以使得插入过后这个序列其长度为k的任意子序列和相同。

解题思路:

这题不要求插入最少的数,那么我们直接搞起:

我们先用set记录这个序列中有多少个不同的数字,如果这个序列中存在超过k个不同的数字,则没有答案,输出-1。

如果序列中存在小于k个不同的数字,那么我们随便添点1-n中数字,把set中的元素个数补至k个。

然后我们把原序列中的每个数字都替换成set中的序列段即可(因为这个序列段一定含有当前这个数字),最后一共有n*k个数字。

具体看代码:

 1 #include <bits/stdc++.h>
 2 typedef long long LL;
 3 #define pb push_back
 4 const int INF = 0x3f3f3f3f;
 5 const double eps = 1e-8;
 6 const int mod = 1e9+7;
 7 const int maxn = 1e5+10;
 8 using namespace std;
 9 
10 int a[maxn];
11 
12 int main()
13 {
14     #ifdef DEBUG
15     freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout);
16     #endif
17     
18     int T;
19     scanf("%d",&T);
20     while(T--)
21     {
22         vector<int> vt;
23         set<int> st;
24         int n,k;
25         scanf("%d %d",&n,&k);
26         for(int i=1;i<=n;i++)
27         {
28             scanf("%d",&a[i]);
29             st.insert(a[i]);
30         }
31         if(st.size()>k)
32         {
33             printf("-1\n");
34             continue;
35         }
36         for(int i=1;i<=n;i++)//将set中的元素补至k个
37         {
38             if(st.size()==k) break;
39             if(!st.count(i)) st.insert(i);
40         }
41         printf("%d\n",n*k);
42         for(int i=1;i<=n;i++)//将每个数字替换成set中的序列段
43         {
44             for(auto it : st)
45                 printf("%d ",it);
46         }
47         printf("\n");
48     }
49     
50     return 0;
51 }

-

猜你喜欢

转载自www.cnblogs.com/jiamian/p/12818225.html