fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,x;
  4. vector<pair<int,int>> a;
  5. bool cmp(pair<int,int> a,pair<int,int> b){
  6. if(abs(x-a.first)==abs(x-b.first))
  7. return a.second<b.second;
  8. return abs(x-a.first)<abs(x-b.first);
  9. }
  10. void testcase(){
  11. cin>>n>>x;
  12. a.resize(n);
  13. for(int i=0;i<n;i++){
  14. cin>>a[i].first;
  15. a[i].second=i;
  16. }
  17. sort(a.begin(),a.end(),cmp);
  18. for(auto i:a) cout<<i.first<<" ";
  19. }
  20. int main(){
  21. ios_base::sync_with_stdio(false);
  22. cin.tie(NULL); cout.tie(NULL);
  23. int T=1;cin>>T;
  24. while(T--){
  25. testcase();
  26. cout<<"\n";
  27. }
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout