結果

問題 No.2665 Minimize Inversions of Deque
ユーザー haihamabossuhaihamabossu
提出日時 2024-03-08 21:57:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 994 bytes
コンパイル時間 2,144 ms
コンパイル使用メモリ 207,708 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-08 21:57:12
合計ジャッジ時間 5,855 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 36 ms
6,676 KB
testcase_02 AC 35 ms
6,676 KB
testcase_03 AC 35 ms
6,676 KB
testcase_04 AC 35 ms
6,676 KB
testcase_05 AC 34 ms
6,676 KB
testcase_06 AC 34 ms
6,676 KB
testcase_07 AC 34 ms
6,676 KB
testcase_08 AC 35 ms
6,676 KB
testcase_09 AC 35 ms
6,676 KB
testcase_10 AC 35 ms
6,676 KB
testcase_11 AC 35 ms
6,676 KB
testcase_12 AC 35 ms
6,676 KB
testcase_13 AC 34 ms
6,676 KB
testcase_14 AC 35 ms
6,676 KB
testcase_15 AC 34 ms
6,676 KB
testcase_16 AC 34 ms
6,676 KB
testcase_17 AC 35 ms
6,676 KB
testcase_18 AC 35 ms
6,676 KB
testcase_19 AC 7 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 3 ms
6,676 KB
testcase_22 AC 5 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 3 ms
6,676 KB
testcase_26 AC 3 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 3 ms
6,676 KB
testcase_29 AC 3 ms
6,676 KB
testcase_30 AC 45 ms
6,676 KB
testcase_31 AC 42 ms
6,676 KB
testcase_32 AC 42 ms
6,676 KB
testcase_33 AC 46 ms
6,676 KB
testcase_34 AC 41 ms
6,676 KB
testcase_35 AC 42 ms
6,676 KB
testcase_36 AC 42 ms
6,676 KB
testcase_37 AC 42 ms
6,676 KB
testcase_38 AC 43 ms
6,676 KB
testcase_39 AC 46 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/fenwicktree>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

void solve() {
  ll n;
  cin >> n;
  atcoder::fenwick_tree<ll> bit(n);
  ll cnt = 0;
  deque<ll> ans;
  rep(i, n) {
    ll p;
    cin >> p;
    if (i == 0) {
      bit.add(p - 1, 1);
      ans.push_back(p);
      continue;
    }
    ll low = bit.sum(0, p);
    ll high = i - low;
    bit.add(p - 1, 1);
    if (low == high) {
      cnt += low;
      if (p < ans.front()) {
        ans.push_front(p);
      } else {
        ans.push_back(p);
      }
    } else if (low < high) {
      cnt += low;
      ans.push_front(p);
    } else {
      cnt += high;
      ans.push_back(p);
    }
  }
  cout << cnt << '\n';
  rep(i, n) cout << (i ? " " : "") << ans[i];
  cout << '\n';
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  int T = 1;
  cin >> T;
  for (int t = 0; t < T; t++) {
    solve();
  }
  return 0;
}
0