Codeforces C. Rotation Matching(思維) (Round #648 Div.2)

傳送門

題意: 現有a,b兩個1 ~n 的數組,可以多次選擇任意數k並將b循環向左或向右移動可位,試問最後使a與b的最大匹配對數爲多少?(i = j 並ai = bj即爲匹配)。
在這裏插入圖片描述
思路:

  • 直接找到沒一個數對應的k,並用一個數組f來統計每一個k的貢獻值
  • 假定x在a中位置爲i,在b中位置爲j;若i >= j,則k = i - j, 反之k = i - j + n。
  • 最後ans與每一個出現的k的貢獻值取個max即可。

代碼實現:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int n, ans, tmp;
int a[N], b[N];
map<int, int> c, pos;

signed main()
{
    IOS;

    cin >> n;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
        pos[a[i]] = i;
    }
    for(int i = 1; i <= n; i ++)
        cin >> b[i];

    for(int i = 1; i <= n; i ++){
        int p = pos[b[i]];
        if(p >= i) tmp = p - i;
        else tmp = p - i + n;
        c[tmp] ++;
        ans = max(ans, c[tmp]);
    }

    cout << ans << endl;

    return 0;
}

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