fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define ll long long
  5.  
  6. int main() {
  7. int t;
  8. cin>>t;
  9.  
  10.  
  11. while(t--)
  12. {
  13. int n;
  14. cin>>n;
  15. ll a[n];
  16. multiset<ll> s;
  17.  
  18. for(int i=0;i<n;i++) {cin>>a[i];s.insert(a[i]);}
  19.  
  20.  
  21. vector<vector<int>> ans(2);
  22.  
  23. int flag[2];
  24. flag[0]=1,flag[1]=1;
  25.  
  26. //flag[0]
  27. ll e=*s.begin();
  28. s.erase(s.find(e));
  29. ans[0].pb(e);
  30. int b=1;
  31.  
  32. while(!s.empty() && flag[0]==1)
  33. {
  34. ll val=-1;
  35. if(b==1) {auto e1=prev(s.end()); if(*e1>e) val=*e1;else {flag[0]=0;continue;}}
  36. else { auto e1=s.begin(); if(*e1<e) val=*e1;else {flag[0]=0;continue;} }
  37.  
  38.  
  39. // cout<<val<<" "<<b<<endl;
  40. s.erase(s.find(val));
  41. ans[0].pb(val);
  42. e=val;
  43. b=1-b;
  44. }
  45.  
  46. s.clear();
  47. for(int i=0;i<n;i++) s.insert(a[i]);
  48. e=*s.rbegin();
  49. s.erase(s.find(e));
  50. ans[1].pb(e);
  51. b=0;
  52.  
  53. while(!s.empty() && flag[1]==1)
  54. {
  55. ll val=-1;
  56. if(b==1) {auto e1=prev(s.end()); if(*e1>e) val=*e1;else {flag[1]=0;continue;}}
  57. else { auto e1=s.begin(); if(*e1<e) val=*e1;else {flag[1]=0;continue;} }
  58.  
  59.  
  60. // cout<<val<<" "<<b<<endl;
  61. s.erase(s.find(val));
  62. ans[1].pb(val);
  63. e=val;
  64. b=1-b;
  65. }
  66.  
  67.  
  68. if(flag[0]==1) {for(int i=0;i<ans[0].size();i++) cout<<ans[0][i]<<" ";}
  69. else
  70. {
  71. if(flag[1]==1) {for(int i=0;i<ans[1].size();i++) cout<<ans[1][i]<<" ";}
  72. else cout<<-1;
  73. }
  74. cout<<endl;
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
  82. return 0;
  83. }
  84.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty