fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. int n;
  10. cin >> n;
  11. vector<int> b(n / 2);
  12. vector<int> c(n + 1);
  13. bool done = false;
  14. for(int i = 0; i < n / 2; i++){
  15. int x;
  16. cin >> x;
  17. c[x]++;
  18. if(c[x] > 1){
  19. done = true;
  20. }
  21. b[i] = x;
  22. }
  23. if(done){
  24. cout << -1 << "\n";
  25. return;
  26. }
  27. vector<int> d;
  28. for(int i = 1; i <= n; i++)if(!c[i])d.push_back(i);
  29.  
  30.  
  31. vector<int> ans(n);
  32. set<int> allow;
  33. int idx = 0;
  34. for(int i = 0; i < n; i += 2){
  35. ans[i + 1] = b[idx];
  36. if(d[idx] < b[idx]){
  37. allow.insert(d[idx]);
  38. idx++;
  39. }else{
  40. if(allow.size() == 0 || *(allow.begin()) > b[idx]){
  41.  
  42. cout << -1 << "\n";
  43. return;
  44. }
  45. auto y = allow.lower_bound(b[idx]);
  46. y--;
  47. ans[i] = *y;
  48. allow.erase(y);
  49. allow.insert(d[idx]);
  50. idx++;
  51. }
  52. }
  53. for(int i = 0; i < n; i += 2){
  54. if(ans[i] == 0){
  55. ans[i] = *(allow.begin());
  56. allow.erase(ans[i]);
  57. }
  58. }
  59. for(auto x: ans)cout << x<< " ";
  60. cout << "\n";
  61.  
  62. }
  63.  
  64. int main(){
  65. ios_base::sync_with_stdio(false);
  66. cin.tie(nullptr);
  67.  
  68. int t = 1;
  69. cin >> t;
  70.  
  71. for(int i = 1; i <= t; i++){
  72. solve();
  73. }
  74. return 0;
  75. }
Success #stdin #stdout 0.01s 5288KB
stdin
6
6
4 3 6
4
2 4
8
8 7 2 3
6
6 4 2
4
4 4
8
8 7 4 5
stdout
1 4 2 3 5 6 
1 2 3 4 
-1
3 6 5 4 1 2 
-1
1 8 2 7 6 4 3 5