CF1365C

https://codeforces.com/problemset/problem/1365/C

大概题意:给定两个1~n的全排列,两个排列任意向左轮换or向右轮换,问最大匹配数

题解:可保持一个排列不动,只让一个排列只向左or向右移,可以处理出每个数到达匹配位置的移动次数n,再统计n的众数个数即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<vector>
#include<string>
#include<map>
#include<cstdio>
#include<list>
#include<stack>
#include<unordered_set>
using namespace std;
using ll = long long;
ll T, n, m, k;
ll tmp, a[300005], pos[300005];
int main()
{
cin >> n;
for (ll i = 1; i <= n;i++)
{
cin >> tmp;
pos[tmp] = i;
}
map<ll,ll> ans;
for (ll i = 1; i <= n;i++)
{
cin >> tmp;
pos[tmp] -= i;
if(pos[tmp]<0)
{
pos[tmp] = n + pos[tmp];
}
ans[pos[tmp]]++;
}
ll MAX = -1;
for (auto e:ans)
{
if(e.second>MAX)
{
MAX = e.second;
}
}
cout << MAX << '\n';
return 0;
}