2021ccpc桂林
D - Assumption is All You Need
一题只差痛失打铜,这题队友交给了我是我不争气
cf复现链接
题目
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 to n: A and B, and JB wants you to output a sequence of element swapping operation (xi,yi) on A, so that:
every pair of swapped element forms an inversion pair (i.e. xiAyi when the i-th operation is being performed)
A will become B at the end of the swapping sequence.
or determine it is impossible. Help prove JB’s belief by solving this problem!
Input
There are multiple test cases. The first line of the input contains one integer T indicating the number of test cases. For each test case:
The first line contains one integer n (1≤n≤2021), indicating the number elements in A and B.
The second line contains n distinct integers A1,A2,…,An (1≤Ai≤n), indicating the array A.
The third line contains n distinct integers B1,B2,…,Bn (1≤Bi≤n), indicating the array B.
It is guaranteed that the sum of n in all test cases will not exceed 2021.
Output
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 (0≤k≤n(n−1)2), indicating the length of the swapping sequence. Then, output k line each containing two integers xi and yi (1≤xi<yi≤n), indicating the i-th operation swap(Axi,Ayi).
思路
从1~m最小的位置不正确的开始枚举p,在区间[Bp,Ap]中找到大于p的最小值q然后swap(Ap,Aq),一直重复该操作只到枚举完,如果找不到一个q满足的话输出-1。
已ac代码
构造加线段树,有个测试点一直超时改用线段树来维护区间最小值
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 3000 + 10;
int a[N], n, m, mp[N], pm[N];
typedef pair<int, int> PII;
vector<PII> p;
struct Node
{
int l, r;
int v; // 区间[l, r]的最小值
}tr[N * 4];
void pushup(int u)
{
tr[u].v = min(tr[