結果

問題 No.1854 Limited Bubble Sort
ユーザー wnjxykkkwnjxykkk
提出日時 2022-03-14 20:55:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 2,166 bytes
コンパイル時間 2,291 ms
コンパイル使用メモリ 204,840 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 08:02:48
合計ジャッジ時間 3,837 ms
ジャッジサーバーID
(参考情報)
judge14 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 6 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 7 ms
4,348 KB
testcase_19 AC 6 ms
4,348 KB
testcase_20 AC 7 ms
4,348 KB
testcase_21 AC 5 ms
4,348 KB
testcase_22 AC 9 ms
4,348 KB
testcase_23 AC 6 ms
4,348 KB
testcase_24 AC 11 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do{cout << "\033[32;1m" << #x << "->" ; err(x);} while(0)
void err(){cout << "\033[39;0m" << endl;}
template<template<typename...> class T,typename t,typename... A>
void err(T<t> a,A... x){for (auto v:a) cout << v << ' '; err(x...);}
template<typename T,typename... A>
void err(T a,A... x){cout << a << ' '; err(x...);}
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}
template<class T>
void print(T x,int suc=1)
{
    cout<<x;
    if(suc==1) cout<<'\n';
    else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
    for(int i=0;i<v.size();i++)
        print(v[i],i==(int)(v.size())-1?suc:2);
}


int a[1000];
bool ok=1,suc=1;
vi ans;
void solve(int l,int r) {
	if(l>=r) return ;
	int mxpos;
	for(int i=l;i<=r;++i) {
		if(a[i]>r||a[i]<l) {
			ok=0;
			return ;
		}
		if(a[i]==r) mxpos=i;
	}
	if(mxpos==r) {
		solve(l,r-1);
		return ;
	}
	if(mxpos+1==r) {
		ans.push_back(mxpos);
		swap(a[r-1],a[r]);
		solve(l,r-1);
		return ;
	}
	for(int i=mxpos+1;i<=r;++i) {
		if(a[i]!=i-1) {
			for(int j=i-1;j>=mxpos;--j) {
				swap(a[j],a[j+1]);
				ans.push_back(j);
			}
			for(int j=mxpos+1;j<i;++j) {
				swap(a[j],a[j+1]);
				ans.push_back(j);
			}
			mxpos=i;
		}
	}
	for(int i=mxpos;i<r;++i) {
		swap(a[i],a[i+1]);
		ans.push_back(i);
	}
	// print(ans);
	solve(l,r-1);

}
int n;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int t;
	cin>>t;
	while(t--) {
		cin>>n;
		ans.clear();
		suc=1;
		for(int i=1;i<=n;++i) {
			cin>>a[i];
		}
		int pre=1;
		for(int i=1;i<=n;++i) {
			if(a[i]==i) {
				if(pre!=i) {
					ok=1;
					solve(pre,i);
					if(!ok) {
						suc=0;
						break;
					}
					pre=i+1;
				}
			} else {

			}
		}
		// cout<<pre<<" "<<n<<endl;
		if(pre!=n) solve(pre,n);
		if(!suc) cout<<-1<<endl;
		else {
			// if(ans.size()==0)
			print(ans.size());
			print(ans);
		}
 	}
} 
0