当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

2938:A poor officer

题目描述
There are n soldiers numbered from 1 to n in a troop. They stand in a line according to their numbers, which means the soldier numbered i stands at the ith position. But the officer of this troop is unsatisfied with the arrangement, he decides to rearrange it. After a careful consideration, the officer develops such rearrangement as follows: He thinks up a permutation of 1 to n, and then commands the aith soldier to stand in the ith position in the new arrangement. However, the new sequence still cannot meet the officer’s need. So they have to apply the arrangement repeatedly according to the method the officer thinks up.


For example, there are 5 soldiers in the troop; the permutation in the officer’s mind is 2 1 4 5 3. After the first rearrangement, the troop sequence is 2 1 4 5 3. After the second rearrangement, the sequence becomes 1 2 5 3 4.


Unluckily, after a long time of rearrangements, the officer and the soldiers are very tired but the strict officer is still unsatisfied. The poor officer asks the soldiers, “Who can tell me the least times of rearrangements needed to reach the arrangement I like.” As the smartest soldier you decide to answer the officer’s question.
Here is an example: if there are 5 soldiers in the troop, the permutation in the officer’s mind is 2 1 4 5 3. He wants the troop to be 2 1 3 4 5. The rearrangements are as follows:



输入解释
The input contains several cases. Each case is described as follows:

1st line: n, the number of soldiers (0 < n ≤ 10,000).

2nd line: a1 a2 ... an, the permutation that the troop applies each time.

3rd line: b1 b2 ... bn, the target arrangement the officer is satisfied with.

The last case is followed by a line containing one zero.
输出解释
For each case, output one line which contains the least times needed. You may assume it’s less than 2,000,000,000. If it’s impossible to do that, just output -1.
输入样例
5
2 1 4 5 3
2 1 3 4 5
0
输出样例
3
来自杭电HDUOJ的附加信息
Recommend lcy

该题目是Virtual Judge题目,来自 杭电HDUOJ

源链接: HDU-2938

最后修改于 2020-10-25T22:58:25+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
5000/2000MS(Java/Others) 32768/32768K(Java/Others)