fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int oo = 2e5 + 3;
  5. const int inf = 1e9;
  6.  
  7. int n;
  8. int a[oo];
  9.  
  10. void solve() {
  11. cin >> n;
  12. for (int i = 1; i <= n; i++) {
  13. cin >> a[i];
  14. }
  15.  
  16. sort(a + 1, a + 1 + n);
  17.  
  18. int d = inf;
  19. int pos1 = 0, pos2 = 0;
  20. for (int i = 1; i < n; i++) {
  21. if (a[i + 1] - a[i] < d) {
  22. d = a[i + 1] - a[i];
  23. pos1 = i;
  24. pos2 = i + 1;
  25. }
  26. }
  27.  
  28. vector<int>res;
  29. res.push_back(a[pos1]);
  30.  
  31. for (int i = 1; i <= n; i++) {
  32. if (i == pos1 || i == pos2) {
  33. continue;
  34. }
  35.  
  36. if (a[i] >= a[pos1]) {
  37. res.push_back(a[i]);
  38. }
  39. }
  40.  
  41. for (int i = 1; i <= n; i++) {
  42. if (i == pos1 || i == pos2) {
  43. continue;
  44. }
  45.  
  46. if (a[i] < a[pos1]) {
  47. res.push_back(a[i]);
  48. }
  49. }
  50.  
  51. res.push_back(a[pos2]);
  52.  
  53. for (auto &x : res) {
  54. cout << x <<" ";
  55. }
  56. cout << '\n';
  57. }
  58.  
  59. int main() {
  60.  
  61. int t;
  62. cin >> t;
  63. while (t--) {
  64. solve();
  65. }
  66.  
  67. return 0;
  68. }
Success #stdin #stdout 0s 5324KB
stdin
2
4
4 2 1 2
2
3 1
stdout
2 4 1 2 
1 3