結果

問題 No.2665 Minimize Inversions of Deque
ユーザー Diana773Diana773
提出日時 2024-03-08 21:34:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 2,016 bytes
コンパイル時間 1,749 ms
コンパイル使用メモリ 174,056 KB
実行使用メモリ 7,524 KB
最終ジャッジ日時 2024-03-08 21:34:17
合計ジャッジ時間 6,253 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 35 ms
6,676 KB
testcase_02 AC 35 ms
6,676 KB
testcase_03 AC 34 ms
6,676 KB
testcase_04 AC 34 ms
6,676 KB
testcase_05 AC 35 ms
6,676 KB
testcase_06 AC 34 ms
6,676 KB
testcase_07 AC 34 ms
6,676 KB
testcase_08 AC 34 ms
6,676 KB
testcase_09 AC 37 ms
6,676 KB
testcase_10 AC 34 ms
6,676 KB
testcase_11 AC 34 ms
6,676 KB
testcase_12 AC 34 ms
6,676 KB
testcase_13 AC 35 ms
6,676 KB
testcase_14 AC 34 ms
6,676 KB
testcase_15 AC 34 ms
6,676 KB
testcase_16 AC 34 ms
6,676 KB
testcase_17 AC 34 ms
6,676 KB
testcase_18 AC 34 ms
6,676 KB
testcase_19 AC 7 ms
6,676 KB
testcase_20 AC 4 ms
6,676 KB
testcase_21 AC 4 ms
6,676 KB
testcase_22 AC 4 ms
6,676 KB
testcase_23 AC 4 ms
6,676 KB
testcase_24 AC 4 ms
6,676 KB
testcase_25 AC 4 ms
6,676 KB
testcase_26 AC 3 ms
6,676 KB
testcase_27 AC 4 ms
6,676 KB
testcase_28 AC 4 ms
6,676 KB
testcase_29 AC 4 ms
6,676 KB
testcase_30 AC 101 ms
7,524 KB
testcase_31 AC 86 ms
6,676 KB
testcase_32 AC 102 ms
6,676 KB
testcase_33 AC 96 ms
7,524 KB
testcase_34 AC 86 ms
6,676 KB
testcase_35 AC 69 ms
7,524 KB
testcase_36 AC 70 ms
7,524 KB
testcase_37 AC 78 ms
6,676 KB
testcase_38 AC 83 ms
7,524 KB
testcase_39 AC 89 ms
7,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

template <typename Info>

struct SegmentTree
{
	int n;
	std::vector<Info> info;
	SegmentTree(int n): n(n-1),info(4<<std::__lg(n))
	{
	} 
	SegmentTree(std::vector<Info> init): SegmentTree(init.size())
	{
		std::function<void(int,int,int)> build= [&] (int pl,int l,int r)
		{
			if (l==r)
		    {
		    	info[pl]=init[l];
		    	return;
			}
			int mid=(l+r)>>1;
			build(pl*2,l,mid);
			build(pl*2+1,mid+1,r);
			pull(pl);
		};
		build (1,1,n);
	}
	void pull(int pl)
	{
		info[pl]=info[pl*2]+info[pl*2+1];
	} 
	void modify(int pl,int l,int r,int x,const Info &v)
	{
		if (l==r)
		{
			info[pl]=v;
			return;
		}
		int mid=(l+r)>>1;
		if (x<=mid) modify(pl*2,l,mid,x,v);
		if (x>mid)  modify(pl*2+1,mid+1,r,x,v);
		pull(pl);
	}
	void modify(int x,const Info &v)
	{
		modify(1,1,n,x,v);
	}
	Info rangeQuery(int pl,int l,int r,int x,int y)
	{
		if ((l>y)||(x>r)) 
		  return Info();
		if ((x<=l)&&(r<=y))
		  return info[pl];
		int mid=(l+r)>>1;
        return rangeQuery(pl*2,l,mid,x,y)+rangeQuery(pl*2+1,mid+1,r,x,y);
	}
	Info rangeQuery(int x,int y)
	{
		return rangeQuery(1,1,n,x,y);
	}
	 
}; 

using namespace std;

int _;
int n,l,r;
int x[200010];
int q[500010];
long long anss;

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>_;
	for (int qqw=1; qqw<=_; qqw++)
	{
		cin>>n;
		SegmentTree<int> ran(n+10);
		l=250000; r=l-1;
		anss=0;
		for (int i=1; i<=n; i++)
		{
		  cin>>x[i];
		  int ccf=ran.rangeQuery(1,x[i]);
		  
		  if (ccf<i-1-ccf)
		  {
		  	l--;
		  	q[l]=x[i];
		  	anss=anss+ccf;
		  }
		  if (ccf>i-1-ccf)
		  {
		  	r++;
		  	q[r]=x[i];
		  	anss=anss+i-1-ccf;
		  }
		  if (ccf==i-1-ccf)
		  {
		  	if (i==1)
		  	{
		  		r++;
		  		q[r]=x[i];
		  	}
		  	else
		  	{
		  		if (x[i]<q[l])
		  		{
		  			l--;
		  			q[l]=x[i];
		  		}
		  		else
		  		{
		  			r++;
		  			q[r]=x[i];
		  		}
		  	}
		  	anss=anss+ccf;
		  }
		  
		  ran.modify(x[i],1);
		}
		cout<<anss<<'\n';
		for (int i=l; i<=r; i++)
		  cout<<q[i]<<' ';
		cout<<'\n';
	}

	
	return 0;
}
0