2021ccpc桂林小结

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[
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值