結果

問題 No.2665 Minimize Inversions of Deque
ユーザー 👑 OnjoujiTokiOnjoujiToki
提出日時 2024-03-08 23:40:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,541 bytes
コンパイル時間 1,650 ms
コンパイル使用メモリ 140,008 KB
実行使用メモリ 7,628 KB
最終ジャッジ日時 2024-03-08 23:40:58
合計ジャッジ時間 31,757 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,548 KB
testcase_01 AC 1,719 ms
6,548 KB
testcase_02 AC 1,572 ms
6,676 KB
testcase_03 AC 1,508 ms
6,676 KB
testcase_04 AC 1,447 ms
6,676 KB
testcase_05 AC 1,512 ms
6,676 KB
testcase_06 AC 1,455 ms
6,676 KB
testcase_07 AC 1,411 ms
6,676 KB
testcase_08 AC 1,446 ms
6,676 KB
testcase_09 AC 1,415 ms
6,676 KB
testcase_10 AC 1,449 ms
6,676 KB
testcase_11 AC 1,365 ms
6,676 KB
testcase_12 AC 1,434 ms
6,676 KB
testcase_13 AC 1,397 ms
6,676 KB
testcase_14 AC 1,447 ms
6,676 KB
testcase_15 AC 1,395 ms
6,676 KB
testcase_16 AC 1,403 ms
6,676 KB
testcase_17 AC 1,429 ms
6,676 KB
testcase_18 AC 1,444 ms
6,676 KB
testcase_19 AC 207 ms
6,676 KB
testcase_20 AC 5 ms
6,676 KB
testcase_21 AC 5 ms
6,676 KB
testcase_22 AC 5 ms
6,676 KB
testcase_23 AC 5 ms
6,676 KB
testcase_24 AC 5 ms
6,676 KB
testcase_25 AC 5 ms
6,676 KB
testcase_26 AC 6 ms
6,676 KB
testcase_27 AC 5 ms
6,676 KB
testcase_28 AC 6 ms
6,676 KB
testcase_29 AC 5 ms
6,676 KB
testcase_30 WA -
testcase_31 AC 57 ms
6,676 KB
testcase_32 AC 57 ms
6,676 KB
testcase_33 WA -
testcase_34 AC 57 ms
6,676 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 56 ms
6,676 KB
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}
template <int mod>
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<mod> &p) {
    return os << p.x;
  }
  friend std::istream &operator>>(std::istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};
using mint = ModInt<998244353>;
const int MOD = 998244353;
template <typename T>
std::pair<std::vector<T>, std::vector<T>> get_prime_factor_with_kinds(T n) {
  std::vector<T> prime_factors;
  std::vector<T> 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 <typename T>
std::vector<T> get_divisors(T x, bool sorted = true) {
  std::vector<T> 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 <intrin.h>
#endif

#if __cplusplus >= 202002L
#include <bit>
#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 <class S, auto op, auto e>
struct segtree {
  static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,
                "op must work as S(S, S)");
  static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,
                "e must work as S()");

#else

template <class S, S (*op)(S, S), S (*e)()>
struct segtree {

#endif

 public:
  segtree() : segtree(0) {}
  explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
  explicit segtree(const std::vector<S> &v) : _n(int(v.size())) {
    size = (int)internal::bit_ceil((unsigned int)(_n));
    log = internal::countr_zero((unsigned int)size);
    d = std::vector<S>(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 <bool (*f)(S)>
  int max_right(int l) const {
    return max_right(l, [](S x) { return f(x); });
  }
  template <class F>
  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 <bool (*f)(S)>
  int min_left(int r) const {
    return min_left(r, [](S x) { return f(x); });
  }
  template <class F>
  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<S> d;

  void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder

template <typename T>
struct DSU {
  std::vector<T> 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 <typename T>
struct FenwickTree {
  std::vector<T> 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 clear() { std::fill(bit.begin(), bit.end(), 0); }
};
FenwickTree<int> bit(200010);
FenwickTree<int> bit2(200010);
void solve() {
  int n;
  std::cin >> n;
  bit.clear();
  bit2.clear();
  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }

  std::deque<int> 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<int> 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();
  }
}
0