답안 #522298

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
522298 2022-02-04T14:26:53 Z amunduzbaev Cat (info1cup19_cat) C++17
100 / 100
482 ms 13848 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array
typedef long long ll;

void solve(){
	int n; cin>>n;
	vector<int> a(n), pos(n);
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;i<n;i++) pos[--a[i]] = i;
	vector<ar<int, 2>> res;
	
	auto add = [&](int i, int j){
		int i_ = n - i - 1, j_ = n - j - 1;
		assert(j != i_);
		swap(a[i], a[j]);
		swap(pos[a[i]], pos[a[j]]);
		res.push_back({i, j});
		swap(a[i_], a[j_]);
		swap(pos[a[i_]], pos[a[j_]]);
	};
	
	vector<int> tmp;
	for(int i=0;i<n/2;i++){
		if(a[i] == i){
			continue;
		}
		int j = pos[i];
		if(j < n/2){
			add(i, j);
		} else {
			if(n - j - 1 < i){
				cout<<-1<<"\n";
				return;
			}
			
			if(n - j - 1 == i){
				tmp.push_back(i);
				continue;
			} else {
				add(i, j);
			}
		}
	}
	
	if((int)tmp.size() & 1){
		cout<<-1<<"\n";
		return;
	}
	
	for(int i=0;i<(int)tmp.size();i+=2){
		add(tmp[i], tmp[i+1]);
		add(tmp[i], pos[tmp[i]]);
	}
	for(int i=1;i<n;i++){
		if(a[i] < a[i-1]){
			cout<<-1<<"\n";
			return;
		}
	}
	
	cout<<(int)res.size()<<" "<<(int)res.size()<<"\n";
	for(auto x : res) cout<<x[0] + 1<<" "<<x[1] + 1<<"\n";
}

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int t; cin>>t;
	while(t--){
		solve();
	}
}

/*

10
4 9 6 3 1 ' 10 8 5 2 7
1 9 6 3 4 ' 7 8 5 2 10
1 2 3 4 5 ' 6 7 8 9 10

6
2 6 4 ' 3 1 5
1 2 3 ' 4 5 6

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 460 KB Output is correct
2 Correct 18 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 332 KB Output is correct
2 Correct 20 ms 460 KB Output is correct
3 Correct 18 ms 460 KB Output is correct
4 Correct 21 ms 716 KB Output is correct
5 Correct 8 ms 568 KB Output is correct
6 Correct 7 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 460 KB Output is correct
2 Correct 18 ms 460 KB Output is correct
3 Correct 482 ms 12908 KB Output is correct
4 Correct 386 ms 10496 KB Output is correct
5 Correct 449 ms 13776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 332 KB Output is correct
2 Correct 20 ms 460 KB Output is correct
3 Correct 18 ms 460 KB Output is correct
4 Correct 21 ms 716 KB Output is correct
5 Correct 8 ms 568 KB Output is correct
6 Correct 7 ms 460 KB Output is correct
7 Correct 482 ms 12908 KB Output is correct
8 Correct 386 ms 10496 KB Output is correct
9 Correct 449 ms 13776 KB Output is correct
10 Correct 426 ms 11428 KB Output is correct
11 Correct 364 ms 9552 KB Output is correct
12 Correct 390 ms 12132 KB Output is correct
13 Correct 440 ms 13848 KB Output is correct
14 Correct 376 ms 11424 KB Output is correct