結果

問題 No.2665 Minimize Inversions of Deque
ユーザー kurehakureha
提出日時 2024-04-13 17:07:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 2,640 bytes
コンパイル時間 2,825 ms
コンパイル使用メモリ 204,024 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-13 17:07:16
合計ジャッジ時間 10,154 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 112 ms
6,944 KB
testcase_02 AC 108 ms
6,940 KB
testcase_03 AC 98 ms
6,944 KB
testcase_04 AC 103 ms
6,940 KB
testcase_05 AC 97 ms
6,940 KB
testcase_06 AC 100 ms
6,940 KB
testcase_07 AC 101 ms
6,948 KB
testcase_08 AC 97 ms
6,940 KB
testcase_09 AC 109 ms
6,944 KB
testcase_10 AC 110 ms
6,940 KB
testcase_11 AC 97 ms
6,940 KB
testcase_12 AC 100 ms
6,944 KB
testcase_13 AC 101 ms
6,940 KB
testcase_14 AC 99 ms
6,944 KB
testcase_15 AC 100 ms
6,940 KB
testcase_16 AC 101 ms
6,940 KB
testcase_17 AC 98 ms
6,944 KB
testcase_18 AC 99 ms
6,940 KB
testcase_19 AC 16 ms
6,944 KB
testcase_20 AC 6 ms
6,944 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 4 ms
6,944 KB
testcase_23 AC 5 ms
6,940 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 5 ms
6,940 KB
testcase_27 AC 4 ms
6,944 KB
testcase_28 AC 5 ms
6,940 KB
testcase_29 AC 5 ms
6,944 KB
testcase_30 AC 144 ms
10,208 KB
testcase_31 AC 119 ms
6,944 KB
testcase_32 AC 128 ms
6,944 KB
testcase_33 AC 135 ms
8,848 KB
testcase_34 AC 120 ms
6,944 KB
testcase_35 AC 97 ms
11,008 KB
testcase_36 AC 91 ms
11,004 KB
testcase_37 AC 150 ms
6,944 KB
testcase_38 AC 118 ms
8,940 KB
testcase_39 AC 126 ms
10,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = V<VV<T>>;
template<class T> using VVVV = VV<VV<T>>;
#define rep(i,n) for(ll i=0ll;i<n;i++)
#define REP(i,a,n) for(ll i=a;i<n;i++)
const long long INF = (1LL << 60);
const long long mod99 = 998244353;
const long long mod107 = 1000000007;
const long long mod = mod99;
#define eb emplace_back
#define pb eb
#define be(v) (v).begin(),(v).end()
#define all(i,v) for(auto& i : v)
#define all2(i,j,v) for(auto& [i,j] : v)
template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; }
template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; }

// cin.tie(nullptr);
// ios::sync_with_stdio(false);
// cout << fixed << setprecision(20);


template <typename T, T (*OP)(T, T), T (*E)()>
struct SegmentTree { 
  vector<T> seg;
  ll seg_size;//葉の数
    
  SegmentTree(ll N) : seg(4*N, E()), seg_size() {
    seg_size = 1;
    while(seg_size < N) seg_size *= 2;
  }
  SegmentTree(const vector<T> a) : seg(4*a.size(), E()), seg_size() {
    ll N = a.size();
    seg_size = 1;
    while(seg_size < N) seg_size *= 2;
    rep(i,N) seg[i + seg_size - 1] = a[i];
    for(ll i = seg_size-2; i >= 0; i --){
      seg[i] = OP(seg[2*i + 1], seg[2*i + 2]);
    }
  }
  
  void update(ll idx, T x){
    idx += seg_size - 1;
    seg[idx] = x;
    
    while(idx > 0){
      idx = (idx - 1) / 2;
      seg[idx] = OP(seg[2*idx + 1], seg[2*idx + 2]);
    }
  }
 

  T quere(ll a, ll b) { return quere_sub(a, b, 0, 0, seg_size); }
  T quere_sub(ll a, ll b, ll idx, ll left, ll right){
    if(a >= right || b <= left) return E();
    if(a <= left && b >= right) return seg[idx];
  
    T v1 = quere_sub(a, b, 2 * idx + 1, left, (left + right) / 2);
    T v2 = quere_sub(a, b, 2 * idx + 2, (left + right) / 2, right);
    return OP(v1, v2);
  }
  
};


template<typename S>
S e(){return 0;}
template<typename S>
S op(S L, S R){return L+R;}


void solve(){
  ll n;
  cin >> n;
  SegmentTree<ll,op,e> seg(n+1);
  deque<ll> dq;
  ll ans = 0;
  rep(i, n){
    ll a;
    cin >> a;
    ll f = seg.quere(0, a);
    ll b = seg.quere(a+1, n+1);
    seg.update(a, 1);
    ans += min(f, b);
    if(f < b || dq.empty()) dq.push_front(a);
    else if(f > b || dq.front() < a) dq.push_back(a);
    else dq.push_front(a);
  }
  cout << ans << endl;
  for(auto a:dq) cout << a << " ";
  cout << endl;
}





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