結果

問題 No.2665 Minimize Inversions of Deque
ユーザー cho435cho435
提出日時 2024-03-08 21:34:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 805 bytes
コンパイル時間 4,771 ms
コンパイル使用メモリ 266,604 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-08 21:34:26
合計ジャッジ時間 9,131 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 100 ms
6,676 KB
testcase_02 AC 90 ms
6,676 KB
testcase_03 AC 85 ms
6,676 KB
testcase_04 AC 84 ms
6,676 KB
testcase_05 AC 93 ms
6,676 KB
testcase_06 AC 84 ms
6,676 KB
testcase_07 AC 85 ms
6,676 KB
testcase_08 AC 84 ms
6,676 KB
testcase_09 AC 84 ms
6,676 KB
testcase_10 AC 82 ms
6,676 KB
testcase_11 AC 92 ms
6,676 KB
testcase_12 AC 85 ms
6,676 KB
testcase_13 AC 85 ms
6,676 KB
testcase_14 AC 83 ms
6,676 KB
testcase_15 AC 85 ms
6,676 KB
testcase_16 AC 82 ms
6,676 KB
testcase_17 AC 82 ms
6,676 KB
testcase_18 AC 81 ms
6,676 KB
testcase_19 AC 13 ms
6,676 KB
testcase_20 AC 4 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 4 ms
6,676 KB
testcase_25 AC 3 ms
6,676 KB
testcase_26 AC 4 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 4 ms
6,676 KB
testcase_29 AC 4 ms
6,676 KB
testcase_30 AC 90 ms
6,676 KB
testcase_31 AC 76 ms
6,676 KB
testcase_32 AC 78 ms
6,676 KB
testcase_33 AC 77 ms
6,676 KB
testcase_34 AC 75 ms
6,676 KB
testcase_35 AC 78 ms
6,676 KB
testcase_36 AC 81 ms
6,676 KB
testcase_37 AC 75 ms
6,676 KB
testcase_38 AC 86 ms
6,676 KB
testcase_39 AC 80 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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();
}
0