結果
問題 | No.2665 Minimize Inversions of Deque |
ユーザー | kokosei |
提出日時 | 2024-03-08 21:46:41 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 66 ms / 2,000 ms |
コード長 | 956 bytes |
コンパイル時間 | 3,062 ms |
コンパイル使用メモリ | 250,580 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-09-29 19:26:57 |
合計ジャッジ時間 | 7,024 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/fenwicktree> using namespace std; using ll = long long; int N; int P[202020]; void solve(){ cin >> N; atcoder::fenwick_tree<int> bt(N); deque<int> ans; ll x = 0; for(int i = 0;i < N;i++){ int p; cin >> p; p--; int f = bt.sum(0, p), b = bt.sum(p, N); if(f > b){ ans.push_back(p + 1); }else if(f < b){ ans.push_front(p + 1); }else{ if(ans.size() && ans.front() > p){ ans.push_front(p + 1); }else{ ans.push_back(p + 1); } } x += min(f, b); bt.add(p, 1); } cout << x << endl; for(int i = 0;i < N;i++){ cout << ans.front() << " \n"[i + 1 == N]; ans.pop_front(); } } int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--)solve(); return 0; }