結果
問題 | No.2665 Minimize Inversions of Deque |
ユーザー |
|
提出日時 | 2024-03-08 21:34:12 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 98 ms / 2,000 ms |
コード長 | 805 bytes |
コンパイル時間 | 4,168 ms |
コンパイル使用メモリ | 254,664 KB |
最終ジャッジ日時 | 2025-02-20 02:13:15 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i<(int)(n);i++) using mint = atcoder::modint998244353; void solve(){ int n; cin>>n; vector<int> p(n); rep(i,n) cin>>p.at(i); rep(i,n) p.at(i)--; atcoder::fenwick_tree<int> fw(n); deque<int> ans; ans.push_back(p.at(0)); fw.add(p.at(0),1); ll res=0; for(int i=1;i<n;i++){ int btt=fw.sum(p.at(i),n); int ftt=i-btt; if(ftt==btt){ if(ans.front()<p.at(i)) ans.push_back(p.at(i)); else ans.push_front(p.at(i)); }else if(ftt<btt){ ans.push_front(p.at(i)); }else{ ans.push_back(p.at(i)); } res+=min(ftt,btt); fw.add(p.at(i),1); } cout<<res<<"\n"; for(auto x:ans){ cout<<x+1<<" "; } cout<<"\n"; } int main(){ int t; cin>>t; rep(i,t) solve(); }