codeforces C. Rotation Matching

在這裏插入圖片描述

題目

題意:

給你兩個序列a,ba,b你可以將bb中的元素進行循環左/右移,問a,ba,b中在ii位子上的相等的數量最高的時候,數量是多少。

思路:

我們可以進行排序,然後我們可以得到兩個一樣的數組,然後我們通過他們原來位子的差值來知道如果此時要相等需要右移多少個位子,然後差值的數量最多的那個差值就是需要右移的位數了,所以此時那個數量就是最後的答案,需要注意的是如果差值是負數的時候,說明此時右移的時候超了,所以要再從11開始計數,所以計算的時候要變成(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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章