CF187A.Permutations

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.

One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.

Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.

快乐的 PMP 是一名大一新生,正在学习算法问题。他非常喜欢玩算法类游戏。

一位高年级学长给了快乐的 PMP 一个有趣的游戏:他被给出两个由 11 到 nn 的数字构成的排列,需要将第一个排列转换为第二个排列。在一次操作中,他可以将排列末尾的数字移除,并将其重新插入到任意位置——既可以插入到任意两个相邻数字之间,也可以将其放置在排列的最开头。

快乐的 PMP 已有一个能解决该问题的算法,但效率不够高。他想知道:将第一个排列转换为第二个排列所需的最少操作次数是多少?

输入格式

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.

Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.

Next line describe the second permutation in the same format.

第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5)—— 表示给定的两个排列中数字的个数。

下一行包含 nn 个用空格分隔的整数——第一个排列。数字 11 到 nn 中的每个数在该排列中恰好出现一次。

再下一行以相同格式描述第二个排列。

输出格式

Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.

输出一个整数,表示将第一个排列转换为第二个排列所需的最少移动次数。

输入输出样例

  • 输入#1

    3
    3 2 1
    1 2 3

    输出#1

    2
  • 输入#2

    5
    1 2 3 4 5
    1 5 2 3 4

    输出#2

    1
  • 输入#3

    5
    1 5 2 3 4
    1 2 3 4 5

    输出#3

    3

说明/提示

In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.

In the second sample, he removes number 5 and inserts it after 1.

In the third sample, the sequence of changes are like this:

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

So he needs three moves.

在第一个样例中,他将列表末尾的数字 1 移除,并将其放置到开头;之后,他取数字 2 并将其插入到 1 和 3 之间。

在第二个样例中,他移除数字 5,并将其插入到 1 之后。

在第三个样例中,变化过程如下:

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

因此,他需要三次操作。

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

首页