I. Permutations
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
2
5
1 2 3 4 5
1 5 2 3 4
1
5
1 5 2 3 4
1 2 3 4 5
3
简单的总结这种思想,就是自定义规则,我不管目标序列有多乱,我就以他为标准,我就认可他是上升的序列,然后其他序列都得按照他的标准来排序。。。
#include <stdio.h>
#define maxn 200005
int a[maxn];
int b[maxn];
int c[maxn];
int main()
{
int n,m;
while(scanf("%d",&n)!=-1)
{
for (int k=1;k<=n;k++)
scanf("%d",&a[k]);
for (int i=1;i<=n;i++)
{
scanf("%d",&m);
b[m]=i;
}
for (int i=1;i<=n;i++)
{
c[i]=b[a[i]];
//printf("%d ",c[i]);
}
//printf("\n");
int j;
for (j=2;j<=n;j++)
{
if (c[j-1]>c[j])
{
//printf("%d\n",j);
break;
}
}
printf("%d\n",n-j+1);
}
return 0;
}