Untitled

 avatar
unknown
c_cpp
a year ago
1.1 kB
13
Indexable
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int tt, tc;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int& u : a) cin >> u;

    sort(a.begin(), a.end());
    deque<int> dq;
    for (int i = 0; i < (n / 2) + (n & 1); i++) dq.push_back(a[i]);
    for (int i = n - 1; i >= (n / 2) + (n & 1); --i) dq.push_back(a[i]);

    vector<int> b;
    bool turn = false;
    while ((int)dq.size()) {
        if (!turn) {
            b.push_back(dq.front());
            dq.pop_front();
        } else {
            b.push_back(dq.back());
            dq.pop_back();
        }
        turn = !turn;
    }

    int mountains = 0;
    for (int i = 1; i < n - 1; i++) if (b[i] > b[i - 1] && b[i] > b[i + 1]) mountains += 1;
    
    if (mountains < k) cout << -1 << "\n";
    else {
        for (int u : b) cout << u << " ";
        cout << "\n";
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    tt = 1, tc = 1; cin >> tt;
    while (tt--) solve(), tc++;
}
Editor is loading...
Leave a Comment