cf构造题(4/50) C. Rotation Matching

题目链接
在这里插入图片描述
题意:
给定两个数组由1-n组成,将一个数组整体向右或向左平移(末尾移至开头),求两个数组最多能匹配数字的数量
题目分析:
每次移动完再判断肯定超时,我们发现数组由1-n组成,所以说每个数字所能匹配的数字移动的距离是固定的,我们可以分别计算得出每个数字匹配成功所要移动的距离,最多的数量就是最多的移动相同的距离。
代码:

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define int long long
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
typedef set<int>::iterator SIT;
int dx[]={0,-1,0,1},dy[]={-1,0,1,0};
int gcd(int a,int b) {return b?gcd(b,a%b):a;}
int lcm(int a,int b) {return a/gcd(a,b)*b;}
const double eps=1e-6;
const int INF=0x3f3f3f3f,mod=998244353;
const int N=1e6+10;
int n;
int a[N],b[N];
map<int,int>id;
map<int,int>dis;
signed main(){
    fast;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        id[a[i]]=i;
    }
    for(int i=0;i<n;i++){
        cin>>b[i];
        dis[id[b[i]]-i]++;
        dis[-(n+i-id[b[i]])]++;
    }
    int ans=0;
    for(int i=-n;i<n;i++)
        ans=max(ans,dis[i]);
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值