题目
题意:
给你两个序列 a , b a,b a,b你可以将 b b b中的元素进行循环左/右移,问 a , b a,b a,b中在 i i i位子上的相等的数量最高的时候,数量是多少。
思路:
我们可以进行排序,然后我们可以得到两个一样的数组,然后我们通过他们原来位子的差值来知道如果此时要相等需要右移多少个位子,然后差值的数量最多的那个差值就是需要右移的位数了,所以此时那个数量就是最后的答案,需要注意的是如果差值是负数的时候,说明此时右移的时候超了,所以要再从 1 1 1开始计数,所以计算的时候要变成 ( x + m o d ) % m o d (x+mod) \% mod (x+mod)%mod。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
int xxxxxxxxx = 1;
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
inline void debug(ll x) {cout << xxxxxxxxx++ << " " << x << endl;}
inline void debugs(string s) {cout << s << endl;}
const int maxn = 2e5 + 10;
struct NODE {
int val;
int id;
bool friend operator < (NODE a, NODE b) {
return a.val < b.val;
}
};
NODE a[maxn], b[maxn];
int cnt[maxn] = {0};
int main() {
int n; read(n);
for (int i = 0; i < n; i++) read(a[i].val), a[i].id = i;
for (int i = 0; i < n; i++) read(b[i].val), b[i].id = i;
sort(a, a + n);
sort(b, b + n);
int ans = 0;
for (int i = 0; i < n; i++) {
cnt[(a[i].id - b[i].id + n) % n]++;
ans = max(ans, cnt[(a[i].id - b[i].id + n) % n]);
}
outi(ans);
return 0;
}