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

题目链接:https://codeforces.com/contest/1365/problem/C

题意

有两个大小为 $n$ 的排列,可以循环左移或右移任意次,问最多有多少对同一值在同一位置。

题解

计算两个排列中同一值相差的距离,取个数最多的那个即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; cin >> n;
    vector<int> id[n];
    for (int _ = 0; _ < 2; _++) {
        for (int i = 0; i < n; i++) {
            int x; cin >> x;
            --x;
            id[x].push_back(i);
        }
    }
    int cnt[n] = {};
    for (int i = 0; i < n; i++) {
        int j = (id[i][0] - id[i][1] + n) % n;
        ++cnt[j];
    }
    cout << *max_element(cnt, cnt + n);
}
原文地址:https://www.cnblogs.com/Kanoon/p/13063323.html