当前位置: 首页 > news >正文

【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;
}

http://www.mrgr.cn/news/52639.html

相关文章:

  • 速盾:cdn有防御吗?
  • 动态规划(1)斐波那契数列模型
  • mysql--索引
  • Hi3061M——VL53L0X激光测距(IIC)(同样适用于其他MCU)
  • JAVA八股
  • ts 中 type 和 interface 的区别
  • 【vivado】vivado联合modelsim仿真
  • VUE组件间的通信方式都有哪些
  • MySQL 免密登录的几种配置方式
  • CTFHub | HTTP协议 - 请求方式 | 题解实操
  • rollup.js 插件实现原理与自定义
  • C++之默认拷贝函数
  • Apache Calcite - 将Sql转换为关系表达式
  • 【顺序表的模拟实现Java】
  • 计算机视觉——人像的分割与无缝融合
  • Vue 3 的不同版本总结
  • 有源滤波器(三)
  • 基于SSM医药垃圾分类管理系统【附源码】
  • springboot定时任务
  • C#笔记(1)