跳到正文

题解

题解:CF1365C Rotation Matching

Gartjock约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年10月6日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 1 分钟
获赞
4
题目
CF1365C

版本

  1. v2当前
  2. v1

本题中的 aa , bb 数列可以看作是一个圆筒上的圈,无论怎么转都可以得到相同的映射,并且 aa , bb 中的数字完全相同,那么我们就可以用 unordered_map 对数值和位置建立映射,查看 aa , bb 中每个元素位置相同所需移动的距离,出现最多的距离数就是答案。

代码如下:

C++
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
#define mod 998244353
using namespace std;
const int N=1e6+6,inf=2e18;
typedef pair<int,int>PII;
int n,a[N],b[N],c[N],ans;
unordered_map<int,int>mp;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		cin>>b[i],mp[b[i]]=i;//a,b中元素相同,对b中进行映射
	for(int i=1;i<=n;i++){
		if(mp[a[i]]>=i)c[mp[a[i]]-i]++;
		else c[mp[a[i]]+n-i]++;//统计距离数,统一计算向右移动的距离,向左也行,只要统一即可
	}
	ans=*max_element(c,c+n+1);
	cout<<ans;
	return 0;
}
原文