列表

详情


NC219808. Permutation

描述

You are given two permutations and of length .
A permutation is a sequence of length integers from to , in which all the numbers occur exactly once. For example, are permutations, and are not.
There are two types of operations as follow:
  • Remove the first integer in . Then append any integer at the end of .
  • Choose an and modify it to any integer.
You have to find the minimum number of operations to make and the same.
Be attention, you do not have to keep in a permutation during the process.

输入描述

The first line contains an integer  — the length of  and  .
The second line contains integers .
The third line contains integers .
It is guaranteed that and are both permutations.

输出描述

Output the minimum number of operations to make  and  the same.

示例1

输入:

5
5 2 3 4 1
5 3 2 1 4

输出:

3

说明:

For the first example, there is a possible way:
1. Remove the first integer in a_{} and append 4, now a_{} becomes [2,3,4,1,4]_{}.
2. modify a_1 to 5_{}, now a_{} becomes [5,3,4,1,4]_{}.
3. modify a_3 to 2_{}, now a_{} becomes [5,3,2,1,4]_{}.
So it only takes 3_{} operations to convert a_{} into b_{}.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 682ms, 内存消耗: 6532K, 提交时间: 2021-03-27 15:04:38

#include<iostream>
#include<algorithm>
using namespace std;
int a[1000001],b[1000001];
int main(){
	int n,c;cin>>n;
	for(int i=0;i<n;++i)cin>>c,a[c]=i;
	for(int i=0;i<n;++i){
		cin>>c;
		if(a[c]-i>=0)b[a[c]-i]++;
	}
	int m=n;
	for(int i=0;i<n;++i)m=min(m,n-b[i]); 
		cout<<m<<endl;
	return 0;
} 

上一题