結果
問題 | No.2665 Minimize Inversions of Deque |
ユーザー | ripity |
提出日時 | 2024-03-08 21:28:07 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 84 ms / 2,000 ms |
コード長 | 773 bytes |
コンパイル時間 | 2,290 ms |
コンパイル使用メモリ | 207,756 KB |
実行使用メモリ | 6,528 KB |
最終ジャッジ日時 | 2024-09-29 18:57:59 |
合計ジャッジ時間 | 6,676 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 |
ソースコード
#include <bits/stdc++.h>using namespace std;#include <atcoder/fenwicktree>using namespace atcoder;void solve() {int n;cin >> n;vector<int> P(n);for( int i = 0; i < n; i++ ) {cin >> P[i];}long long x = 0;deque<int> A;fenwick_tree<long long> fw(n);for( int i = 0; i < n; i++ ) {long long invl = fw.sum(0, P[i]-1);long long invr = fw.sum(P[i]-1, n);if( A.empty() || invl < invr || ( invl == invr && P[i] < A.front() ) ) {A.push_front(P[i]);x += invl;}else {A.push_back(P[i]);x += invr;}fw.add(P[i]-1, 1LL);}cout << x << "\n";for( int i = 0; i < n; i++ ) {cout << A[i] << " ";}cout << "\n";}int main() {int T;cin >> T;while(T--) {solve();}}