#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template std::ostream &operator<<(std::ostream &os, const std::pair &p) { os << p.first << " " << p.second; return os; } template std::istream &operator>>(std::istream &is, std::pair &p) { is >> p.first >> p.second; return is; } template std::ostream &operator<<(std::ostream &os, const std::vector &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } template std::istream &operator>>(std::istream &is, std::vector &v) { for (T &in : v) is >> in; return is; } template struct ModInt { int x; ModInt() : x(0) {} ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt &operator^=(long long p) { // quick_pow here:3 ModInt res = 1; for (; p; p >>= 1) { if (p & 1) res *= *this; *this *= *this; } return *this = res; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } ModInt operator^(long long p) const { return ModInt(*this) ^= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } explicit operator int() const { return x; } // added by QCFium ModInt operator=(const int p) { x = p; return ModInt(*this); } // added by QCFium ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return ModInt(u); } friend std::ostream &operator<<(std::ostream &os, const ModInt &p) { return os << p.x; } friend std::istream &operator>>(std::istream &is, ModInt &a) { long long x; is >> x; a = ModInt(x); return (is); } }; using mint = ModInt<998244353>; const int MOD = 998244353; template std::pair, std::vector> get_prime_factor_with_kinds(T n) { std::vector prime_factors; std::vector cnt; // number of i_th factor for (T i = 2; i * i <= n; i++) { if (n % i == 0) { prime_factors.push_back(i); cnt.push_back(0); while (n % i == 0) n /= i, cnt[(int)prime_factors.size() - 1]++; } } if (n > 1) prime_factors.push_back(n), cnt.push_back(1); assert(prime_factors.size() == cnt.size()); return {prime_factors, cnt}; } template std::vector get_divisors(T x, bool sorted = true) { std::vector res; for (T i = 1; i <= x / i; i++) if (x % i == 0) { res.push_back(i); if (i != x / i) res.push_back(x / i); } if (sorted) std::sort(res.begin(), res.end()); return res; } // source atcoder #ifdef _MSC_VER #include #endif #if __cplusplus >= 202002L #include #endif namespace atcoder { namespace internal { #if __cplusplus >= 202002L using std::bit_ceil; #else unsigned int bit_ceil(unsigned int n) { unsigned int x = 1; while (x < (unsigned int)(n)) x *= 2; return x; } #endif int countr_zero(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } constexpr int countr_zero_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } } // namespace internal } // namespace atcoder namespace atcoder { #if __cplusplus >= 201703L template struct segtree { static_assert(std::is_convertible_v>, "op must work as S(S, S)"); static_assert(std::is_convertible_v>, "e must work as S()"); #else template struct segtree { #endif public: segtree() : segtree(0) {} explicit segtree(int n) : segtree(std::vector(n, e())) {} explicit segtree(const std::vector &v) : _n(int(v.size())) { size = (int)internal::bit_ceil((unsigned int)(_n)); log = internal::countr_zero((unsigned int)size); d = std::vector(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) const { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) const { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() const { return d[1]; } template int max_right(int l) const { return max_right(l, [](S x) { return f(x); }); } template int max_right(int l, F f) const { assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template int min_left(int r) const { return min_left(r, [](S x) { return f(x); }); } template int min_left(int r, F f) const { assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; } // namespace atcoder template struct DSU { std::vector f, siz; DSU(int n) : f(n), siz(n, 1) { std::iota(f.begin(), f.end(), 0); } T leader(T x) { while (x != f[x]) x = f[x] = f[f[x]]; return x; } bool same(T x, T y) { return leader(x) == leader(y); } bool merge(T x, T y) { x = leader(x); y = leader(y); if (x == y) return false; siz[x] += siz[y]; f[y] = x; return true; } T size(int x) { return siz[leader(x)]; } }; template struct FenwickTree { std::vector bit; int n; FenwickTree(int _n) : n(_n), bit(_n, 0) { std::fill(bit.begin(), bit.end(), 0); } T sum(int r) { T ret = 0; for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r]; return ret; } T sum(int l, int r) { assert(l <= r); return sum(r) - sum(l - 1); } // [l, r] void add(int idx, T delta) { for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta; } }; void solve() { int n; std::cin >> n; std::vector a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } FenwickTree bit(200010); FenwickTree bit2(200010); std::deque dq; for (int i = 0; i < n; i++) { int greater_cnt = bit.sum(a[i] + 1, 200009); int less_cnt = bit.sum(0, a[i]); if (greater_cnt < less_cnt) { dq.push_back(a[i]); } else if (greater_cnt > less_cnt) { dq.push_front(a[i]); } else { if (dq.empty()) { dq.push_back(a[i]); } else { if (dq.front() < a[i]) { dq.push_back(a[i]); } else { dq.push_front(a[i]); } } } bit.add(a[i], 1); } std::vector ans; for (int i = 0; i < n; i++) { ans.push_back(dq.front()); dq.pop_front(); } // calculate inversion number int res = 0; for (int i = 0; i < n; i++) { res += bit2.sum(ans[i] + 1, 200009); bit2.add(ans[i], 1); } std::cout << res << '\n'; for (int i = 0; i < n; i++) { std::cout << ans[i] << " \n"[i + 1 == n]; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t = 1; std::cin >> t; while (t--) { solve(); } }