-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE_Beautiful_Palindromes.cpp
More file actions
87 lines (78 loc) · 1.89 KB
/
E_Beautiful_Palindromes.cpp
File metadata and controls
87 lines (78 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, k;
cin >> n >> k;
vector<int> v(n);
set<int> st;
for(int i=1;i<=n;i++) st.insert(i);
for(int i=0;i<n;i++) {
cin >> v[i];
if(st.find(v[i]) != st.end()) st.erase(v[i]);
}
vector<int> temp(st.begin(), st.end());
int sz = temp.size();
vector<int> ans;
if(sz >= 3) {
int q = k / sz;
int rem = k % sz;
while(q--) {
ans.insert(ans.end(), temp.begin(), temp.end());
}
for(int i=0;i<rem;i++) ans.push_back(temp[i]);
}
else if(sz == 2) {
int ss = -1;
for(int i=1;i<=n;i++) {
if(i != temp[0] && i != temp[1]) {
ss = i;
break;
}
}
for(int i=0;i<(k/3);i++) {
ans.push_back(temp[0]);
ans.push_back(temp[1]);
ans.push_back(ss);
}
for(int i=0;i<(k%3);i++) ans.push_back(temp[i]);
}
else if(sz == 1) {
int ff = -1, ss = -1;
for(int i=1;i<=n;i++) {
if(i != temp[0] && i != v[n-1]) {
ff = i;
break;
}
}
for(int i=1;i<=n;i++) {
if(i != temp[0] && i != ff) {
ss = i;
break;
}
}
temp.push_back(ff);
temp.push_back(ss);
for(int i=0;i<(k/3);i++) {
ans.push_back(temp[0]);
ans.push_back(temp[1]);
ans.push_back(ss);
}
for(int i=0;i<(k%3);i++) ans.push_back(temp[i]);
}
else {
for(int i=0;i<min(k, n);i++) ans.push_back(v[i]);
}
for(auto &i : ans) cout << i << " ";
cout << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _;
cin >> _;
while (_-->0) {
solve();
}
return 0;
}