結果
問題 |
No.2665 Minimize Inversions of Deque
|
ユーザー |
![]() |
提出日時 | 2024-04-01 10:02:42 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 53 ms / 2,000 ms |
コード長 | 1,031 bytes |
コンパイル時間 | 2,305 ms |
コンパイル使用メモリ | 200,560 KB |
最終ジャッジ日時 | 2025-02-20 18:46:16 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/fenwicktree> using namespace std; using namespace atcoder; void fast_io() { ios::sync_with_stdio(false); std::cin.tie(nullptr); } void solve() { int n; cin >> n; fenwick_tree<int> fw(n + 2); deque<int> ans; long long inv = 0; for (int i = 0; i < n; i++) { int p; cin >> p; int l_cnt = fw.sum(0, p), r_cnt = fw.sum(p + 1, n + 2); if (l_cnt < r_cnt) { ans.push_front(p); inv += l_cnt; } else if (l_cnt > r_cnt) { ans.push_back(p); inv += r_cnt; } else if (p < ans.front()) { ans.push_front(p); inv += l_cnt; } else { ans.push_back(p); inv += r_cnt; } fw.add(p, 1); } cout << inv << "\n"; for (int i = 0; i < n; i++) { cout << ans[i] << (i + 1 == n ? "\n" : " "); } } int main() { fast_io(); int t; cin >> t; for (; t--;) { solve(); } }