CF#119 Permutations(思维)

Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
3
3 2 1
1 2 3
output
2
input
5
1 2 3 4 5
1 5 2 3 4
output
1
input
5
1 5 2 3 4
1 2 3 4 5
output
3

题意:每次可以移动最后的数到前面位置,问多少次后可以从a数组变成b数组

思路:首先明确,最多移动n次就可以完成,其实每次移动一个数就可以确定一个数的位置.也就是,当我们找到一个数,它应该在它后面的数的后面时,那么至少得操作到它后面那个数时才能停止.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath> 
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 2e6+5;
const int ff = 0x3f3f3f3f;
map<string,int> mp;
map<string,int>::iterator it;

int n;
int a[maxn],b[maxn],num[maxn];

int main()
{
	cin>>n;
	for(int i = 1;i<= n;i++)
		scanf("%d",&a[i]);
	for(int i = 1;i<= n;i++)
	{
		scanf("%d",&b[i]);
		num[b[i]] = i;
	}
	
	for(int i = 1;i< n;i++)
	{
		if(num[a[i]]> num[a[i+1]])
		{
			cout<<n-i<<endl;
			return 0;
		}
	}
	
	cout<<0<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值