CF2262D.PLUSworld
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Farmer John is building PLUSworld, a giant barn complex with n barns numbered from 1 to n. For each 1≤i≤n, there is currently a one-way walkway from barn i to barn ai.
Before opening PLUSworld, Farmer John wants to reconfigure the roads so that, for every 1≤i≤n, the road from barn i points to barn bi. The target roads have the special property that each road points to its own barn or to a higher-numbered barn (i≤bi).
To do this, he sends Bessie to one barn of his choice. Let p be the barn where Bessie is currently located. Bessie may perform the following operations any number of times:
- Operation 1: Increase the destination of the road from the current barn by 1. More formally, set ap:=ap+1. This operation may only be performed if ap<n.
- Operation 2: Follow the road from the current barn. More formally, set p:=ap.
Operation 1 changes the array a, but Bessie stays at the same barn. Operation 2 changes Bessie's current barn, but does not change the array.
Your goal is to make the array a equal to the array b.
Determine whether this is possible. If it is possible, output any valid starting barn and any sequence of operations that makes a=b.
It can be shown that if a solution exists, then there exists one using at most 2n2 operations.
农夫约翰正在建造 PLUSworld,一个拥有 n 座谷仓(编号从 1 到 n)的巨型谷仓综合体。对每个 1≤i≤n,目前存在一条从谷仓 i 指向谷仓 ai 的单向通道。
在 PLUSworld 开放之前,农夫约翰希望重新配置这些通道,使得对每个 1≤i≤n,从谷仓 i 出发的通道都指向谷仓 bi。目标通道具有如下特殊性质:每条通道要么指向其自身的谷仓,要么指向编号更大的谷仓(即 i≤bi)。
为此,他派贝茜前往他选定的一座谷仓。设 p 为贝茜当前所在的谷仓。贝茜可以任意次数地执行以下两种操作:
- 操作 1:将当前谷仓出发的通道终点编号加 1。更准确地说,令 ap:=ap+1。该操作仅当 ap<n 时允许执行。
- 操作 2:沿当前谷仓出发的通道前进。更准确地说,令 p:=ap。
操作 1 会修改数组 a,但贝茜仍停留在同一座谷仓;操作 2 会改变贝茜当前所在的谷仓,但不改变数组 a。
你的目标是使数组 a 变为数组 b。
请判断该目标是否可达。若可达,请输出任意一个合法的起始谷仓编号,以及任意一个能使 a=b 的操作序列。
可以证明:若解存在,则必存在一个使用至多 2n2 次操作的解。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤1000).
The second line contains n integers a1,a2,…,an (1≤ai≤n).
The third line contains n integers b1,b2,…,bn (i≤bi≤n).
It is guaranteed that the sum of n2 over all test cases does not exceed 10002.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤1000)。
第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n)。
第三行包含 n 个整数 b1,b2,…,bn(i≤bi≤n)。
保证所有测试用例的 n2 之和不超过 10002。
输出格式
For each test case, if it is impossible to make a equal to b, output a single integer −1.
Otherwise, output two lines.
On the first line, output two integers o and p — the number of operations you will perform and the initial index you choose (0≤o≤2n2, 1≤p≤n).
On the second line, output o integers c1,c2,…,co (1≤ci≤2), where ci is the type of the i-th operation.
If ci=1, you perform operation 1 and set ap:=ap+1. This operation may only be used if ap<n at that moment.
If ci=2, you perform operation 2 and set p:=ap.
After performing all o operations, the array a must be equal to b.
If there are multiple valid answers, you may output any of them.
对于每个测试用例,若无法使 a 等于 b,则输出单个整数 −1。
否则,输出两行。
第一行输出两个整数 o 和 p —— 你将执行的操作次数以及初始选择的下标(满足 0≤o≤2n2,1≤p≤n)。
第二行输出 o 个整数 c1,c2,…,co(其中 1≤ci≤2),ci 表示第 i 次操作的类型。
- 若 ci=1,则执行操作 1,并将 ap 赋值为 ap+1。该操作仅在当前时刻满足 ap<n 时才允许使用。
- 若 ci=2,则执行操作 2,并将 p 赋值为 ap。
执行完全部 o 次操作后,数组 a 必须等于 b。
若存在多个合法答案,输出任意一个即可。
输入输出样例
输入#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 1. She increases a1 from 1 to 2, follows the road to barn 2, and increases a2 from 2 to 3. The resulting array is [2,3,3], which is equal to b.
For the second test case, a1=2>b1=1. Since road destinations can only be increased, it is impossible to make a1 equal to b1, so the answer is −1.
For the third test case, Bessie starts at barn 1. She changes a1 from 1 to 2 and follows the road to barn 2. She then changes a2 from 1 to 3 and moves to barn 3, changes a3 from 1 to 4 and moves to barn 4, and finally changes a4 from 4 to 5. The resulting array is [2,3,4,5,5], which is equal to b.
对于第一个测试用例,贝茜从谷仓 1 出发。她将 a1 从 1 增加到 2,然后沿道路前往谷仓 2,再将 a2 从 2 增加到 3。最终得到的数组为 [2,3,3],与 b 相等。
对于第二个测试用例,a1=2>b1=1。由于道路只能通向编号更大的谷仓(即目的地编号只能增大),因此无法使 a1 变为 b1,答案为 −1。
对于第三个测试用例,贝茜从谷仓 1 出发。她先将 a1 从 1 改为 2,然后沿道路前往谷仓 2;接着将 a2 从 1 改为 3,前往谷仓 3;再将 a3 从 1 改为 4,前往谷仓 4;最后将 a4 从 4 改为 5。最终得到的数组为 [2,3,4,5,5],与 b 相等。
输入解题思路,AI测评打分。不知道怎么写?