您的位置:首页 > 其它

bnuoj_20924 Permutations

2016-07-07 18:16 274 查看
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.

input:

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.

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

sample input:

3

3 2 1

1 2 3

5

1 2 3 4 5

1 5 2 3 4

5

1 5 2 3 4

1 2 3 4 5

sample output:





3

//相当于寻找前面几个数有序的情况下后面的数插入
#include<stdio.h>
int a[200010],b[200010];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int k=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
if(b[i]==a[k]) k++;
}
printf("%d\n",n-k+1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: