#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章