【ICPC】The 2021 CCPC Guilin Onsite (XXII Open Cup, Grand Prix of EDG) D

Assumption is All You Need

#模拟 #构造

题目描述

JB holds the belief that assumption is all you need to solve a problem. In order to prove that, JB has given you two permutations of numbers from 1 1 1 to n n n: A A A and B B B, and JB wants you to output a sequence of element swapping operation ( x i , y i ) (x_i,y_i) (xi,yi) on A A A, so that:

  1. every pair of swapped element forms an inversion pair ( i . e . i.e. i.e. x i ≤ y i ≤ x_i \leq y_i \leq xiyiand A x i ≥ A y i A_{x_i} \geq A_{y_i} AxiAyi when the i i i-th operation is being performed)
  2. A A A will become B B B at the end of the swapping sequence.

or determine it is impossible. Help prove JB’s belief by solving this problem!

输入格式

There are multiple test cases. The first line of the input contains one integer T T T indicating the number of test cases. For each test case:

The first line contains one integer n n n ( 1 ≤ n ≤ 2   021 1 \le n \le 2\,021 1n2021), indicating the number elements in A A A and B B B.

The second line contains n n n distinct integers A 1 , A 2 , … , A n A_1,A_2,\dots,A_n A1,A2,,An ( 1 ≤ A i ≤ n 1 \le A_i \le n 1Ain), indicating the array A A A.

The third line contains n n n distinct integers B 1 , B 2 , … , B n B_1,B_2,\dots,B_n B1,B2,,Bn ( 1 ≤ B i ≤ n 1 \le B_i \le n 1Bin), indicating the array B B B.

It is guaranteed that the sum of n n n in all test cases will not exceed 2   021 2\,021 2021.

输出格式

For each test case, if there doesn’t exist a sequence, output the one line containing one integer “-1”.

Otherwise, in the first line output one integer k k k ( 0 ≤ k ≤ n ( n − 1 ) 2 0 \le k \le \frac{n(n-1)}{2} 0k2n(n1)), indicating the length of the swapping sequence. Then, output k k k line each containing two integers x i x_i xi and y i y_i yi ( 1 ≤ x i ≤ y i ≤ n 1 \leq x_i \leq y_i \leq n 1xiyin), indicating the i i i-th operation swap ( A x i , A y i ) \text{swap}(A_{x_i},A_{y_i}) swap(Axi,Ayi).

样例 #1

样例输入 #1

3
2
1 2
2 1
4
4 1 2 3
1 3 2 4
8
8 7 6 5 4 3 2 1
1 8 7 6 5 4 3 2

样例输出 #1

-1
2
1 2
2 4
7
7 8
6 7
5 6
4 5
3 4
2 3
1 2

解法

观察到 n ≤ 1 0 3 n\leq 10^3 n103,并且最多交换 n ( n − 1 ) 2 \frac{n(n-1)}{2} 2n(n1) 次,容易想到 n 2 n^2 n2模拟。

而每次交换只能交换逆序对,把值较小的交换到前面来,因此我们要尽可能的让大的数放到前面来,这样才更有机会被交换。

因此,最优的交换策略之一,就是 i i i之后的 j   ( j > i ) j \ (j >i) j (j>i)满足 a j < a i   ∩   a j ≤ b i a_j <a_i \ \cap \ a_j \leq b_i aj<ai  ajbi a j a_j aj换到前面来,如果交换之后,仍然出现 a i < a j a_i < a_j ai<aj的情况,则无解。

代码

void solve() {
	int n;
	std::cin >> n;

	std::vector<int>a(n + 1), b(n + 1);
	for (int i = 1; i <= n; ++i) {
		std::cin >> a[i];
	}

	for (int i = 1; i <= n; ++i) {
		std::cin >> b[i];
	}

	std::vector<pii>res;
	for (int i = 1; i <= n; ++i) {
		if (a[i] == b[i]) continue;

		if (a[i] < b[i]) {
			std::cout << -1 << "\n";
			return;
		}

		int j = i + 1;

		while (j <= n && a[i] != a[j]) {
			if (a[j] < a[i] && a[j] <= b[i]) {
				std::swap(a[i], a[j]);
				res.push_back({ i,j });
			}
			j++;
		}
	}

	std::cout << res.size() << "\n";
	for (auto& [x, y] : res) {
		std::cout << x << " " << y << "\n";
	}

}

signed main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	std::cin >> t;

	while (t--) {
		solve();
	}

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值