#648 (Div. 2)C. Rotation Matching

题目描述

After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let’s call them a and b.
Note that a permutation of n elements is a sequence of numbers a1,a2,…,an, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements ai and bj is said to match if:
i=j, that is, they are at the same index.
ai=bj
His two disciples are allowed to perform the following operation any number of times:
choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c1:=c2,c2:=c3,…,cn:=c1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c1:=cn,c2:=c1,…,cn:=cn−1 simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.

Input

The first line of the input contains a single integer n (1≤n≤2⋅105) — the size of the arrays.
The second line contains n integers a1, a2, …, an (1≤ai≤n) — the elements of the first permutation.
The third line contains n integers b1, b2, …, bn (1≤bi≤n) — the elements of the second permutation.

Output

Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.

Examples

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

Note

For the first case: b can be shifted to the right by k=1. The resulting permutations will be {1,2,3,4,5} and {1,2,3,4,5}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won’t exceed 1.
For the third case: b can be shifted to the left by k=1. The resulting permutations will be {1,3,2,4} and {2,3,1,4}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won’t exceed 2.

题目大意

给出两个数组,a[]和b[](1<=a[i],b[i]<=n,且a[]和b[]中没有重复的数),可以将数组进行整体的向左/向右平移,求最多能找到多少对配对的数(如果a[i]和b[j]配对,那么i==j、a[i]==b[j])。

题目分析

可以用一个数组pos来记录下a[i]的位置i,再算出a[i]与相应的b[]中的数配对需要平移的距离dis,并记录此时平移dis距离能得到多少配对数。
最后再遍历所有的距离找到最大值即可。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set> 
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=2e5+5;
int a[N],b[N],pos[N];
map<int,int> ma; 
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		pos[a[i]]=i;    //记录a[i]的位置
	}
	for(int j=1;j<=n;j++)
	cin>>b[j];
	
	for(int i=1;i<=n;i++)
	{
		int dis=pos[b[i]]-i;  //让a[j]和b[i]配对需要平移的距离
		if(dis<0) dis+=n;     //保证dis在区间[0,n-1]内
		ma[dis]++;            //平移该距离得到的配对数+1
	}
	int ans=0;
	for(auto it:ma)
	{
		ans=max(ans,it.second);  //遍历所有距离得到的配对数,取最大值
	}
	cout<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/106612457
今日推荐