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

这篇博客探讨了一个涉及两个排列的问题,其中目标是通过有限次的整体移动找到最大匹配元素对数。给定两个长度为n的排列a和b,我们需要找出在允许对a或b进行任意次数的循环移位操作后,最多可以有多少对元素在相同位置上相等。博客提供了输入输出示例,并展示了一个C++解决方案来计算最大匹配数。
摘要由CSDN通过智能技术生成

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
inputCopy
5
1 2 3 4 5
2 3 4 5 1
outputCopy
5
inputCopy
5
5 4 3 2 1
1 2 3 4 5
outputCopy
1
inputCopy
4
1 3 2 4
4 2 3 1
outputCopy
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.

因为是整体移动,所以就算距离就行了

#include <iostream>
#include <algorithm>
#include <set>
#include <cstring>
using namespace std;
#define int long long
int a[200200];
int b[200200];
int c[200200];
signed main()
{

    int n, num, cou = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> num;
        a[num] = i;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> num;
        int len = i - a[num];
        if (len < 0)
            len += n;
        b[len]++;
        if (b[len] > cou)
            cou = b[len];
    }
    cout << cou << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值