A8474 | Permutations
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line contains a single integer $n$ ( $1<=n<=2·10^{5}$ ) — 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.
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.
输出格式
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted