AT_awtf2026algo_d.Adj Swap Lex Max
入门
通过率:0%
时间限制:3.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given permutations P and Q of (1,2,…,N). Here, P is lexicographically not greater than Q.
You can perform the following operation zero or more times.
- Choose two adjacent elements of P and swap them. Here, the following conditions must be satisfied.
- P after the operation is still lexicographically not greater than Q.
- Let (x,y) be the two values being swapped. The pair (x,y) has never been swapped in previous operations. Here, the order of x,y does not matter. That is, once (x,y) has been swapped, neither (x,y) nor (y,x) can be swapped again.
Find the lexicographically greatest permutation that P can become in the end.
Solve T cases for each input.
给你两个 (1,2,…,N) 的排列 P 和 Q,其中 P 在字典序上不大于 Q。
你可以执行以下操作零次或多次:
- 选择 P 中两个相邻的元素并交换它们。该操作需满足以下条件:
- 操作后的 P 在字典序上仍不大于 Q;
- 设被交换的两个值为 (x,y),则该数对 (x,y) 在之前的所有操作中均未被交换过(注意:(x,y) 与 (y,x) 被视为同一数对;即一旦 (x,y) 被交换过,则今后既不允许交换 (x,y),也不允许交换 (y,x))。
求 P 最终所能变成的字典序最大的排列。
对每组输入,求解 T 个测试用例。
输入格式
The input is given from Standard Input in the following format:
T
case1
case2
⋮
caseT
Each test case is given in the following format:
N
P1 P2 … PN
Q1 Q2 … QN
输入从标准输入中按以下格式给出:
T
case1
case2
⋮
caseT
每个测试用例按以下格式给出:
N
P1 P2 … PN
Q1 Q2 … QN
输出格式
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).
- Swap (2,3), resulting in P=(1,3,2).
- Swap (1,3), resulting in P=(3,1,2).
In the second test case, it is optimal to perform no operations at all.
Constraints
- 1≤T≤500000
- 2≤N≤106
- P is a permutation of (1,2,…,N).
- Q is a permutation of (1,2,…,N).
- P is lexicographically not greater than Q.
- The sum of N over the T cases is at most 106.
- All input values are integers.
样例 1 解释:
在第一个测试用例中,执行如下操作是最优的:
- 初始时 P=(1,2,3)。
- 交换 (2,3),得到 P=(1,3,2)。
- 交换 (1,3),得到 P=(3,1,2)。
在第二个测试用例中,最优策略是完全不执行任何操作。
约束条件
- 1≤T≤500000
- 2≤N≤106
- P 是 (1,2,…,N) 的一个排列。
- Q 是 (1,2,…,N) 的一个排列。
- P 的字典序不大于 Q。
- 所有 T 个测试用例的 N 值之和不超过 106。
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?