fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #ifndef M
  4. #define M 1000000007
  5. #endif
  6. #ifndef ONLINE_JUDGE
  7. #define gc getchar
  8. #else
  9. #define gc getchar_unlocked
  10. #endif
  11. typedef pair<int,int>pp;
  12. typedef std::vector<pp> vpp;
  13. typedef long long ll;
  14. typedef long double ld;
  15. #ifndef pb
  16. #define pb push_back
  17. #endif
  18. int min(int x,int y){return(x<y)?x:y;}
  19. int max(int x,int y){return(x>y)?x:y;}
  20. inline long long int read_int()
  21. {
  22. char c = gc();
  23. while(c<'0' || c>'9')
  24. c = gc();
  25. long long int ret = 0;
  26. while(c>='0' && c<='9')
  27. {
  28. ret = (ret<<3) + (ret<<1) + c - 48;
  29. c = gc();
  30. }
  31. return ret;
  32. }
  33. bool cmpfun(ll a ,ll b)
  34. {
  35. ll c1=__builtin_popcount (a),c2=__builtin_popcount (b);
  36. if(c1==c2)
  37. return(a>b);
  38. return(c2<c1);
  39. }
  40. int main(int argc, char const *argv[])
  41. {
  42. ll t,n,a[100005]={0};
  43. scanf("%lld",&t);
  44. while(t--)
  45. {
  46. n=read_int();
  47. for (int i = 0; i < n; ++i)
  48. {
  49. a[i]=read_int();
  50. }
  51. sort(a,a+n,cmpfun);
  52. for (int i = 0; i < n; ++i)
  53. {
  54. printf("%lld ",a[i]);
  55. }
  56. printf("\n");
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0s 4124KB
stdin
1
4
1 2 3 4
stdout
3 4 2 1