結果

問題 No.2665 Minimize Inversions of Deque
ユーザー kotatsugamekotatsugame
提出日時 2024-03-08 21:07:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 894 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 75,940 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-09-29 18:50:48
合計ジャッジ時間 3,820 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<cassert>
#include<atcoder/fenwicktree>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;cin>>T;
	for(;T--;)
	{
		int N;cin>>N;
		atcoder::fenwick_tree<int>BIT(N);
		long long x=0;
		vector<int>L,R;
		for(int i=0;i<N;i++)
		{
			int P;cin>>P;P--;
			int l=BIT.sum(0,P);
			int r=i-l;
			bool fl;
			if(l<r)fl=true;
			else if(r<l)fl=false;
			else
			{
				if(L.empty())
				{
					if(R.empty())fl=true;
					else if(P<R[0])fl=true;
					else fl=false;
				}
				else
				{
					if(P<L.back())fl=true;
					else fl=false;
				}
			}
			if(fl)
			{
				L.push_back(P);
				x+=l;
			}
			else
			{
				R.push_back(P);
				x+=r;
			}
			BIT.add(P,1);
		}
		cout<<x<<"\n";
		reverse(L.begin(),L.end());
		for(int r:R)L.push_back(r);
		for(int i=0;i<N;i++)cout<<L[i]+1<<(i+1==N?"\n":" ");
	}
}
0