Codeforces Round #648 (Div. 2) C. Rotation Matching

Codeforces Round #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≤2e5) — 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

水題~
我們保留每個數變化後的位置,對每個數現在的位置,有左移和右移兩種可能,那就分別記錄一下即可,最後遍歷一遍找最大值即可,AC代碼如下:

#include<bits/stdc++.h>
using namespace std;

main(){
    int n;
    cin>>n;
    map<int,int>m,cnt;
    int a[n],b[n],ans=0;
    for(int i=0;i<n;i++) cin>>a[i],m[a[i]]=i;
    for(int i=0;i<n;i++) cin>>b[i],cnt[m[b[i]]-i]++,cnt[-(n+i-m[b[i]])]++;
    for(int i=-n;i<n;i++) ans=max(ans,cnt[i]);
    cout<<ans;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章