CF1365C 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.

输入

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.

输出

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

样例

输入1

5
1 2 3 4 5
2 3 4 5 1

输出1

5

输入2

5
5 4 3 2 1
1 2 3 4 5

输出2

1

输入3

4
1 3 2 4
4 2 3 1

输出3

2

提示

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.

题意

给定两个排列,可分别向左或向右循环移动0次或多次,求位置相同且值相同的数字(下称匹配)最多有几对。

思路

因为向右移动第二个排列与向左移动第一个排列是等效的,所以可以只移动第一个排列。
容易想到模拟方法,即因为向左或向右移动都可以枚举到所有情况,所以设向右移动,每移动一次计算匹配个数。
分析上述方案发现,移动需要n次操作,每次移动计算匹配个数需要n次操作,时间复杂度为O(n^2),超时。
通过对上述方案的分析发现,一个元素能否匹配只取决于这个元素在两个排列中的初末位置,且只与坐标之差(位置的距离)有关,且每个元素只有一个距离,当不同元素具有相同距离时,这些元素必在某一次移动中全部匹配,必在其他移动中全不匹配;当不同元素具有不同距离时,这些元素在任意一次移动中都不会同时匹配或同时不匹配。
答案仅存在在具有相同距离的不同元素中。对每一种距离,计算拥有这一距离的元素的个数,最大值即为答案。
取得第二个排列的元素在第一个排列中的下标并计算距离可以O(1)实现,计算匹配个数需要n次,计算最大值需要n次,复杂度为O(n)。
实现思路见代码。

代码

//克服WA难,终得AC
#include"bits/stdc++.h"
#define ll long long
using namespace std;
const int N=2e5+10;
const int inf=0x3f3f3f3f;
const double eps=1e-7;

ll n;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin>>n;
  //idx[i]:元素i在第一个排列中的下标,cont[i]:拥有距离i的元素的个数
  vector<ll> idx(n+10);
  vector<ll> cont(n+10);
  for(ll i=0; i<n; ++i) {
    ll t;
    cin>>t;
    idx[t]=i;
  }
  ll ans=1;
  for(ll i=0; i<n; ++i) {
    ll t;
    cin>>t;
    //对距离计数,同时计算最大值
    ans=max(++cont[(idx[t]-i+n)%n],ans);//idx[t]取得当前元素在第一个排列中的下标,idx[t]-i计算当前元素的距离,(idx[t]-i+n)%n将向左移动等效为向右移动
  }
  cout<<ans<<endl;

  return 0;
}
posted @ 2021-08-06 17:03  夏午Sharve  阅读(41)  评论(0)    收藏  举报