• C. Rotation Matching


    C. Rotation Matching

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    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,…,ana1,a2,…,an, 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 aiai and bjbj is said to match if:

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

    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:=c1c1:=c2,c2:=c3,…,cn:=c1 simultaneously. Likewise, a single cyclic shift to the right on any permutation cc is an operation that sets c1:=cn,c2:=c1,…,cn:=cn−1c1:=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 nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the size of the arrays.

    The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤n)(1≤ai≤n) — the elements of the first permutation.

    The third line contains nn integers b1b1, b2b2, ..., bnbn (1≤bi≤n)(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

    Copy

    5
    1 2 3 4 5
    2 3 4 5 1
    

    output

    Copy

    5

    input

    Copy

    5
    5 4 3 2 1
    1 2 3 4 5
    

    output

    Copy

    1

    input

    Copy

    4
    1 3 2 4
    4 2 3 1
    

    output

    Copy

    2

    Note

    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.

    =========================================================================

    只对b进行平移是可以的,只对b进行右移也是可以的,那么就统计出来每个数字偏移的距离即可

    取其最大值

    1. #include
    2. #include
    3. #include
    4. # include
    5. #include
    6. #define mo 998244353;
    7. using namespace std;
    8. typedef long long int ll;
    9. int pre[200000+10];
    10. int now[200000+10];
    11. int cnt[200000+10];
    12. int main()
    13. {
    14. int n;
    15. cin>>n;
    16. for(int i=1;i<=n;i++)
    17. {
    18. int x;
    19. cin>>x;
    20. pre[x]=i;
    21. }
    22. for(int i=1;i<=n;i++)
    23. {
    24. cin>>now[i];
    25. if(i-pre[now[i]]>=0)
    26. {
    27. cnt[i-pre[now[i]]]++;
    28. }
    29. else
    30. {
    31. cnt[i-pre[now[i]]+n]++;
    32. }
    33. }
    34. int ans=0;
    35. for(int i=0;i<=n;i++)
    36. {
    37. ans=max(ans,cnt[i]);
    38. }
    39. cout<
    40. return 0;
    41. }

  • 相关阅读:
    LeetCode常见题型——树
    程序员转业指南 - 当文员
    linux上的代码在windows上运行
    机器学习 | MATLAB实现MLP多层感知机模型设计
    openstack wallaby 对应 需要的python版本 3.8.18
    90.(前端)增加商品分类显示——Cascader 级联选择器的使用
    【牛客面试必刷TOP101】Day8.BM33 二叉树的镜像和BM36 判断是不是平衡二叉树
    深度学习(2):图片文字识别
    为什么使用 Virtual DOM
    JVM基础 -> 什么是STW?
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126267079