結果

問題 No.2665 Minimize Inversions of Deque
ユーザー inksamuraiinksamurai
提出日時 2024-03-08 21:41:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,389 bytes
コンパイル時間 4,469 ms
コンパイル使用メモリ 267,968 KB
実行使用メモリ 12,284 KB
最終ジャッジ日時 2024-03-08 21:41:54
合計ジャッジ時間 8,349 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 38 ms
6,676 KB
testcase_02 AC 37 ms
6,676 KB
testcase_03 AC 37 ms
6,676 KB
testcase_04 AC 37 ms
6,676 KB
testcase_05 AC 38 ms
6,676 KB
testcase_06 AC 37 ms
6,676 KB
testcase_07 AC 38 ms
6,676 KB
testcase_08 AC 38 ms
6,676 KB
testcase_09 AC 38 ms
6,676 KB
testcase_10 AC 37 ms
6,676 KB
testcase_11 AC 37 ms
6,676 KB
testcase_12 AC 37 ms
6,676 KB
testcase_13 AC 36 ms
6,676 KB
testcase_14 AC 36 ms
6,676 KB
testcase_15 AC 38 ms
6,676 KB
testcase_16 AC 37 ms
6,676 KB
testcase_17 AC 37 ms
6,676 KB
testcase_18 AC 36 ms
6,676 KB
testcase_19 AC 7 ms
6,676 KB
testcase_20 AC 3 ms
6,676 KB
testcase_21 AC 4 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 4 ms
6,676 KB
testcase_28 AC 3 ms
6,676 KB
testcase_29 AC 3 ms
6,676 KB
testcase_30 AC 87 ms
11,732 KB
testcase_31 AC 76 ms
7,428 KB
testcase_32 AC 80 ms
7,644 KB
testcase_33 AC 82 ms
10,888 KB
testcase_34 AC 74 ms
6,676 KB
testcase_35 AC 71 ms
12,284 KB
testcase_36 AC 72 ms
12,284 KB
testcase_37 AC 73 ms
7,408 KB
testcase_38 AC 75 ms
10,796 KB
testcase_39 AC 79 ms
12,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int) a.size())
#define vec(...) vector<__VA_ARGS__>
#define _3TpP2FO ios::sync_with_stdio(0),cin.tie(0);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

int op(int l,int r){
	return l+r;
}

int e(){
	return 0;
}

void slv(){
	int n;
	cin>>n;

	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	rep(i,n){
		a[i]-=1;
	}

	int ans=0;
	atcoder::segtree<int,op,e> seg(n);
	vi qs(n);
	rep(i,n){
		int v=a[i];
		// tavshi ro chavagdo
		int l=seg.prod(0,v);
		// boloshi
		int r=seg.prod(v,n);
		if(l==r){
			qs[i]=1;
		}else if(l<r){
			qs[i]=0;
		}else{
			qs[i]=2;
		}
		ans+=min(l,r);
		seg.set(v,1);
	}

	deque<int> dq;
	dq.pb(a[0]);
	rng(i,1,n){
		if(qs[i]==1){
			if(a[i]<dq[0]){
				dq.push_front(a[i]);
			}else{
				dq.pb(a[i]);
			}
		}else if(qs[i]==0){
			dq.push_front(a[i]);
		}else{
			dq.pb(a[i]);
		}
	}

	cout<<ans<<"\n";
	for(auto v:dq) cout<<v+1<<" ";
	print();
}

signed main(){
_3TpP2FO;
	int __t;
	cin>>__t;
	rep(cs,__t){
		slv();
	}
}
0