Codeforces Rotation Matching(思維)

在這裏插入圖片描述
解題思路:
由於第二個數組所有數字是同時向一個方向移動的,求移動後最大匹配項的數目,即可以轉化爲求原始數組中處在同一偏移量的數字個數的最大值,偏移量就等於b數組中某個數組與a數組中相同數字之間的絕對距離

代碼:

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int a[maxn],b[maxn],c[maxn],ans;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i) {
        int temp;
        scanf("%d", &temp);
        a[temp]=i;
    }
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&b[i]);
        int offset=i-a[b[i]];
        if(offset<0) offset+=n;
        c[offset]++;
    }
    for(int i=0;i<n;++i)
        ans=max(ans,c[i]);
    printf("%d\n",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章