結果

問題 No.2665 Minimize Inversions of Deque
ユーザー 矢澤式WINTER矢澤式WINTER
提出日時 2024-03-08 22:48:44
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,526 bytes
コンパイル時間 5,232 ms
コンパイル使用メモリ 312,688 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-08 22:48:55
合計ジャッジ時間 9,721 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,548 KB
testcase_01 AC 100 ms
6,548 KB
testcase_02 AC 94 ms
6,676 KB
testcase_03 AC 89 ms
6,676 KB
testcase_04 AC 87 ms
6,676 KB
testcase_05 AC 90 ms
6,676 KB
testcase_06 AC 89 ms
6,676 KB
testcase_07 AC 93 ms
6,676 KB
testcase_08 AC 89 ms
6,676 KB
testcase_09 AC 88 ms
6,676 KB
testcase_10 AC 87 ms
6,676 KB
testcase_11 AC 88 ms
6,676 KB
testcase_12 AC 87 ms
6,676 KB
testcase_13 AC 88 ms
6,676 KB
testcase_14 AC 87 ms
6,676 KB
testcase_15 AC 89 ms
6,676 KB
testcase_16 AC 90 ms
6,676 KB
testcase_17 AC 97 ms
6,676 KB
testcase_18 AC 90 ms
6,676 KB
testcase_19 AC 14 ms
6,676 KB
testcase_20 AC 3 ms
6,676 KB
testcase_21 AC 3 ms
6,676 KB
testcase_22 AC 3 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 3 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 WA -
testcase_31 AC 42 ms
6,676 KB
testcase_32 AC 40 ms
6,676 KB
testcase_33 WA -
testcase_34 AC 41 ms
6,676 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 43 ms
6,676 KB
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//*/
#include <atcoder/all>
using namespace atcoder;
//*/
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define ALL(x) (x).begin(),(x).end()
#define dbgv(x); for(auto now : x) cout << now << " "; cout << endl;
//using P = pair<int,int>;
using ll = long long;
using ull = unsigned long long;
//*/
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vint;


void solve(){
  int n; cin >> n;
  vint p(n); rep(i,n) cin >> p[i];
  fenwick_tree<int> fw(n+1);
  int c = 0;
  deque<int> ans;
  ans.push_back(p[0]);
  fw.add(p[0],1);
  rep(i,n-1){
    int now = p[i+1];
    int m = fw.sum(0,now);
    int M = ans.size()-m;
    //cout << "l : +" << m << " r : +" << M << endl; 
    if(M > m){
      c += m;
      ans.push_front(now);
    }else if(M < m){
      c += M;
      ans.push_back(now);
    }else{
      //cout << "! : " << now << endl; 
      if(ans[0] > now) ans.push_front(now);
      else ans.push_back(now);
      c += m;
    }
    fw.add(now,1);
  }
  cout << c << endl;
  for(auto now : ans) cout << now << " "; cout << endl;
  //rep(i,ans.size()) cout << ans[i] << (ans.size()-1 == i ? "\n" : " ");
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1; cin >> t;
  rep(_,t) solve();
}
0