Codeforces B. Phoenix and Beauty (构造 / 思维) (Round #638 Div.2)

传送门

题意: 给出一个n个数初始数组a,让你在其中添加一些整数(可以不插,插入元素在1和n之间)来构造新的数组,使其每k个长度的连续子段的和相同。若无法处理就直接输出-1.不必构造最短的序列!
在这里插入图片描述
思路:

  • 如果数的种类数>m,那么不可能构造成功。
  • 现在确定了数的种类<= m了,若<m那么还需要补齐。
  • 现在就可以用这m个数的组合为模板连续循环复制n次即可。

代码实现:

#include<bits/stdc++.h>
//#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int t, n, k;
set<int> st;
vector<int> ans;

signed main()
{
    IOS;

    cin >> t;
    while(t --){
        cin >> n >> k;
        st.clear(); ans.clear();
        for(int i = 0; i < n; i ++){
            int x; cin >> x;
            st.insert(x);
        }
        if(st.size() > k){
            cout << -1 << endl;
            continue;
        }
        int cnt = 1;
        while(st.size() < k) st.insert(cnt ++);
        for(int i = 0; i < n; i ++)
            for(auto it : st)
                ans.push_back(it);
        cout << n * k << endl;
        for(auto it : ans) cout << it << " ";
        cout << endl;

    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Satur9/article/details/107412923