CF1365C.Rotation Matching

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn. Let's call them aa and bb.

Note that a permutation of nn elements is a sequence of numbers a1,a2,…,ana_1, a_2, \ldots, a_n, in which every number from 11 to nn appears exactly once.

The message can be decoded by an arrangement of sequence aa and bb, such that the number of matching pairs of elements between them is maximum. A pair of elements aia_i and bjb_j is said to match if:

  • i=ji = j, that is, they are at the same index.
  • ai=bja_i = b_j

His two disciples are allowed to perform the following operation any number of times:

  • choose a number kk and cyclically shift one of the permutations to the left or right kk times.

A single cyclic shift to the left on any permutation cc is an operation that sets c1:=c2,c2:=c3,…,cn:=c1c_1:=c_2, c_2:=c_3, \ldots, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation cc is an operation that sets c1:=cn,c2:=c1,…,cn:=cn−1c_1:=c_n, c_2:=c_1, \ldots, c_n:=c_{n-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.

在阿西什神秘失踪后,他两位最喜爱的弟子伊希卡(Ishika)和赫里亚德(Hriday)各自获得了一半秘密信息。这两段信息均可表示为一个长度为 nn 的排列。我们将其分别记作 aa 和 bb。

注意:nn 个元素的一个排列是指一个序列 a1,a2,…,ana_1, a_2, \ldots, a_n,其中 11 到 nn 中的每个整数恰好出现一次。

该信息可通过调整序列 aa 和 bb 的相对位置来解码,使得二者之间匹配的元素对数量达到最大。元素对 aia_i 与 bjb_j 被称为匹配,当且仅当满足以下两个条件:

  • i=ji = j,即它们位于相同下标位置;
  • ai=bja_i = b_j。

两位弟子被允许执行以下操作任意多次(包括零次):

  • 选择一个整数 kk,并对其中一个排列向左或向右进行 kk 次循环移位。

对任一排列 cc 执行一次向左循环移位,是指同时执行如下赋值操作:
c1:=c2,  c2:=c3,  …,  cn:=c1c_1 := c_2,\; c_2 := c_3,\; \ldots,\; c_n := c_1。

类似地,对任一排列 cc 执行一次向右循环移位,是指同时执行如下赋值操作:
c1:=cn,  c2:=c1,  …,  cn:=cn−1c_1 := c_n,\; c_2 := c_1,\; \ldots,\; c_n := c_{n-1}。

请帮助伊希卡和赫里亚德求出:在执行任意次数(可能为零)上述操作后,所能达到的最大匹配元素对数量。

输入格式

The first line of the input contains a single integer nn (1≤n≤2⋅105)(1 \le n \le 2 \cdot 10^5) — the size of the arrays.

The second line contains nn integers a1a_1, a2a_2, ..., ana_n (1≤ai≤n)(1 \le a_i \le n) — the elements of the first permutation.

The third line contains nn integers b1b_1, b2b_2, ..., bnb_n (1≤bi≤n)(1 \le b_i \le n) — the elements of the second permutation.

输入的第一行包含一个整数 nn (1≤n≤2⋅105)(1 \le n \le 2 \cdot 10^5) —— 数组的大小。

第二行包含 nn 个整数 a1a_1, a2a_2, ..., ana_n (1≤ai≤n)(1 \le a_i \le n) —— 第一个排列的元素。

第三行包含 nn 个整数 b1b_1, b2b_2, ..., bnb_n (1≤bi≤n)(1 \le b_i \le n) —— 第二个排列的元素。

输出格式

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: bb can be shifted to the right by k=1k = 1. The resulting permutations will be 1,2,3,4,5{1, 2, 3, 4, 5} and 1,2,3,4,5{1, 2, 3, 4, 5}.

For the second case: The operation is not required. For all possible rotations of aa and bb, the number of matching pairs won't exceed 11.

For the third case: bb can be shifted to the left by k=1k = 1. The resulting permutations will be 1,3,2,4{1, 3, 2, 4} and 2,3,1,4{2, 3, 1, 4}. Positions 22 and 44 have matching pairs of elements. For all possible rotations of aa and bb, the number of matching pairs won't exceed 22.

第一种情况:bb 可以向右平移 k=1k = 1 位。得到的排列分别为 1,2,3,4,5{1, 2, 3, 4, 5} 和 1,2,3,4,5{1, 2, 3, 4, 5}。

第二种情况:无需执行该操作。对于 aa 和 bb 的所有可能旋转,匹配元素对的数量均不超过 11。

第三种情况:bb 可以向左平移 k=1k = 1 位。得到的排列分别为 1,3,2,4{1, 3, 2, 4} 和 2,3,1,4{2, 3, 1, 4}。位置 22 和 44 处的元素构成匹配对。对于 aa 和 bb 的所有可能旋转,匹配元素对的数量均不超过 22。

输入解题思路,AI测评打分。不知道怎么写?

首页