CF2262D.PLUSworld

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Farmer John is building PLUSworld, a giant barn complex with nn barns numbered from 11 to nn. For each 1≤i≤n1 \le i \le n, there is currently a one-way walkway from barn ii to barn aia_i.

Before opening PLUSworld, Farmer John wants to reconfigure the roads so that, for every 1≤i≤n1 \le i \le n, the road from barn ii points to barn bib_i. The target roads have the special property that each road points to its own barn or to a higher-numbered barn (i≤bi)(i \le b_i).

To do this, he sends Bessie to one barn of his choice. Let pp be the barn where Bessie is currently located. Bessie may perform the following operations any number of times:

  • Operation 11: Increase the destination of the road from the current barn by 11. More formally, set ap:=ap+1a_p := a_p + 1. This operation may only be performed if ap<na_p \lt n.
  • Operation 22: Follow the road from the current barn. More formally, set p:=app := a_p.

Operation 11 changes the array aa, but Bessie stays at the same barn. Operation 22 changes Bessie's current barn, but does not change the array.

Your goal is to make the array aa equal to the array bb.

Determine whether this is possible. If it is possible, output any valid starting barn and any sequence of operations that makes a=ba=b.

It can be shown that if a solution exists, then there exists one using at most 2n22n^2 operations.

农夫约翰正在建造 PLUSworld,一个拥有 nn 座谷仓(编号从 11 到 nn)的巨型谷仓综合体。对每个 1≤i≤n1 \le i \le n,目前存在一条从谷仓 ii 指向谷仓 aia_i 的单向通道。

在 PLUSworld 开放之前,农夫约翰希望重新配置这些通道,使得对每个 1≤i≤n1 \le i \le n,从谷仓 ii 出发的通道都指向谷仓 bib_i。目标通道具有如下特殊性质:每条通道要么指向其自身的谷仓,要么指向编号更大的谷仓(即 i≤bii \le b_i)。

为此,他派贝茜前往他选定的一座谷仓。设 pp 为贝茜当前所在的谷仓。贝茜可以任意次数地执行以下两种操作:

  • 操作 1:将当前谷仓出发的通道终点编号加 11。更准确地说,令 ap:=ap+1a_p := a_p + 1。该操作仅当 ap<na_p < n 时允许执行。
  • 操作 2:沿当前谷仓出发的通道前进。更准确地说,令 p:=app := a_p。

操作 1 会修改数组 aa,但贝茜仍停留在同一座谷仓;操作 2 会改变贝茜当前所在的谷仓,但不改变数组 aa。

你的目标是使数组 aa 变为数组 bb。

请判断该目标是否可达。若可达,请输出任意一个合法的起始谷仓编号,以及任意一个能使 a=ba = b 的操作序列。

可以证明:若解存在,则必存在一个使用至多 2n22n^2 次操作的解。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤10001\le n\le 1000).

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤n1\le a_i\le n).

The third line contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n (i≤bi≤n\color{red}{i\le b_i}\le n).

It is guaranteed that the sum of n2n^2 over all test cases does not exceed 100021000^2.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤10001\le n\le 1000)。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤n1\le a_i\le n)。

第三行包含 nn 个整数 b1,b2,…,bnb_1,b_2,\ldots,b_n(i≤bi≤n\color{red}{i\le b_i}\le n)。

保证所有测试用例的 n2n^2 之和不超过 100021000^2。

输出格式

For each test case, if it is impossible to make aa equal to bb, output a single integer −1-1.

Otherwise, output two lines.

On the first line, output two integers oo and pp — the number of operations you will perform and the initial index you choose (0≤o≤2n20 \leq o \leq 2n^2, 1≤p≤n1 \leq p \leq n).

On the second line, output oo integers c1,c2,…,coc_1,c_2,\ldots,c_o (1≤ci≤21\le c_i\le 2), where cic_i is the type of the ii-th operation.

If ci=1c_i=1, you perform operation 11 and set ap:=ap+1a_p:=a_p+1. This operation may only be used if ap<na_p \lt n at that moment.

If ci=2c_i=2, you perform operation 22 and set p:=app:=a_p.

After performing all oo operations, the array aa must be equal to bb.

If there are multiple valid answers, you may output any of them.

对于每个测试用例,若无法使 aa 等于 bb,则输出单个整数 −1-1。

否则,输出两行。

第一行输出两个整数 oo 和 pp —— 你将执行的操作次数以及初始选择的下标(满足 0≤o≤2n20 \leq o \leq 2n^2,1≤p≤n1 \leq p \leq n)。

第二行输出 oo 个整数 c1,c2,…,coc_1,c_2,\ldots,c_o(其中 1≤ci≤21\le c_i\le 2),cic_i 表示第 ii 次操作的类型。

  • 若 ci=1c_i=1,则执行操作 11,并将 apa_p 赋值为 ap+1a_p+1。该操作仅在当前时刻满足 ap<na_p \lt n 时才允许使用。
  • 若 ci=2c_i=2,则执行操作 22,并将 pp 赋值为 apa_p。

执行完全部 oo 次操作后,数组 aa 必须等于 bb。

若存在多个合法答案,输出任意一个即可。

输入输出样例

  • 输入#1

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

    输出#1

    5 1
    1 2 1 2 2 
    -1
    12 1
    1 2 1 1 2 1 1 1 2 1 2 2 
    -1
    -1
    2 2
    1 2 
    12 1
    1 1 2 1 2 1 1 2 1 1 2 2 
    0 1
    7 1
    1 2 2 2 2 1 2

说明/提示

For the first test case, Bessie starts at barn 11. She increases a1a_1 from 11 to 22, follows the road to barn 22, and increases a2a_2 from 22 to 33. The resulting array is [2,3,3][2,3,3], which is equal to bb.

For the second test case, a1=2>b1=1a_1=2 \gt b_1=1. Since road destinations can only be increased, it is impossible to make a1a_1 equal to b1b_1, so the answer is −1-1.

For the third test case, Bessie starts at barn 11. She changes a1a_1 from 11 to 22 and follows the road to barn 22. She then changes a2a_2 from 11 to 33 and moves to barn 33, changes a3a_3 from 11 to 44 and moves to barn 44, and finally changes a4a_4 from 44 to 55. The resulting array is [2,3,4,5,5][2,3,4,5,5], which is equal to bb.

对于第一个测试用例,贝茜从谷仓 11 出发。她将 a1a_1 从 11 增加到 22,然后沿道路前往谷仓 22,再将 a2a_2 从 22 增加到 33。最终得到的数组为 [2,3,3][2,3,3],与 bb 相等。

对于第二个测试用例,a1=2>b1=1a_1=2 \gt b_1=1。由于道路只能通向编号更大的谷仓(即目的地编号只能增大),因此无法使 a1a_1 变为 b1b_1,答案为 −1-1。

对于第三个测试用例,贝茜从谷仓 11 出发。她先将 a1a_1 从 11 改为 22,然后沿道路前往谷仓 22;接着将 a2a_2 从 11 改为 33,前往谷仓 33;再将 a3a_3 从 11 改为 44,前往谷仓 44;最后将 a4a_4 从 44 改为 55。最终得到的数组为 [2,3,4,5,5][2,3,4,5,5],与 bb 相等。

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

首页