fork download
  1. #include<bits/stdc++.h>
  2. #define endl "\n"
  3. using namespace std;
  4. typedef long long ll;
  5. const int mod = 1e9 + 7;
  6.  
  7. int main(){
  8. int t;
  9. cin >> t;
  10. while(t--){
  11. int n, x;
  12. cin >> n >> x;
  13. vector<int> a(n);
  14. for(int i=0; i<n; i++)
  15. cin >> a[i];
  16. sort(a.begin(), a.end(), [x](int a, int b){
  17. return abs(x-a) < (x-b);
  18. });
  19. for(int i=0; i<n; i++)
  20. cout << a[i] << " ";
  21. cout << endl;
  22. }
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5292KB
stdin
2
5 7
10 5 3 9 2
5 6
1 2 3 4 5
stdout
10 5 9 3 2 
5 4 3 2 1