AT_awtf2026algo_d.Adj Swap Lex Max

入门

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given permutations PP and QQ of (1,2,…,N)(1,2,\ldots,N). Here, PP is lexicographically not greater than QQ.

You can perform the following operation zero or more times.

  • Choose two adjacent elements of PP and swap them. Here, the following conditions must be satisfied.
    • PP after the operation is still lexicographically not greater than QQ.
    • Let (x,y)(x,y) be the two values being swapped. The pair (x,y)(x,y) has never been swapped in previous operations. Here, the order of x,yx,y does not matter. That is, once (x,y)(x,y) has been swapped, neither (x,y)(x,y) nor (y,x)(y,x) can be swapped again.

Find the lexicographically greatest permutation that PP can become in the end.

Solve TT cases for each input.

给你两个 (1,2,…,N)(1,2,\ldots,N) 的排列 PP 和 QQ,其中 PP 在字典序上不大于 QQ。

你可以执行以下操作零次或多次:

  • 选择 PP 中两个相邻的元素并交换它们。该操作需满足以下条件:
    • 操作后的 PP 在字典序上仍不大于 QQ;
    • 设被交换的两个值为 (x,y)(x,y),则该数对 (x,y)(x,y) 在之前的所有操作中均未被交换过(注意:(x,y)(x,y) 与 (y,x)(y,x) 被视为同一数对;即一旦 (x,y)(x,y) 被交换过,则今后既不允许交换 (x,y)(x,y),也不允许交换 (y,x)(y,x))。

求 PP 最终所能变成的字典序最大的排列。

对每组输入,求解 TT 个测试用例。

输入格式

The input is given from Standard Input in the following format:

TT
case1case_1
case2case_2
⋮\vdots
caseTcase_T

Each test case is given in the following format:

NN
P1P_1 P2P_2 …\ldots PNP_N
Q1Q_1 Q2Q_2 …\ldots QNQ_N

输入从标准输入中按以下格式给出:

TT
case1case_1
case2case_2
⋮\vdots
caseTcase_T

每个测试用例按以下格式给出:

NN
P1P_1 P2P_2 …\ldots PNP_N
Q1Q_1 Q2Q_2 …\ldots QNQ_N

输出格式

For each test case, output the sought permutation.

对于每个测试用例,输出所求的排列。

输入输出样例

  • 输入#1

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

    输出#1

    3 1 2
    2 3 1
    4 2 1 3
    3 2 4 1
    1 3 4 2
    4 2 5 3 1

说明/提示

Sample 1 Explanation:
In the first test case, it is optimal to perform operations as follows.

  • Start with P=(1,2,3)P=(1,2,3).
  • Swap (2,3)(2,3), resulting in P=(1,3,2)P=(1,3,2).
  • Swap (1,3)(1,3), resulting in P=(3,1,2)P=(3,1,2).

In the second test case, it is optimal to perform no operations at all.

Constraints

  • 1≤T≤5000001 \leq T \leq 500000
  • 2≤N≤1062 \leq N \leq 10^6
  • PP is a permutation of (1,2,…,N)(1,2,\ldots,N).
  • QQ is a permutation of (1,2,…,N)(1,2,\ldots,N).
  • PP is lexicographically not greater than QQ.
  • The sum of NN over the TT cases is at most 10610^6.
  • All input values are integers.

样例 1 解释:
在第一个测试用例中,执行如下操作是最优的:

  • 初始时 P=(1,2,3)P=(1,2,3)。
  • 交换 (2,3)(2,3),得到 P=(1,3,2)P=(1,3,2)。
  • 交换 (1,3)(1,3),得到 P=(3,1,2)P=(3,1,2)。

在第二个测试用例中,最优策略是完全不执行任何操作。

约束条件

  • 1≤T≤5000001 \leq T \leq 500000
  • 2≤N≤1062 \leq N \leq 10^6
  • PP 是 (1,2,…,N)(1,2,\ldots,N) 的一个排列。
  • QQ 是 (1,2,…,N)(1,2,\ldots,N) 的一个排列。
  • PP 的字典序不大于 QQ。
  • 所有 TT 个测试用例的 NN 值之和不超过 10610^6。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页