Submission #1129758

#TimeUsernameProblemLanguageResultExecution timeMemory
1129758gygSličnost (COI23_slicnost)C++20
34 / 100
208 ms648 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define arr array 
#define pii pair<int, int>
#define fir first 
#define sec second
const int N = 5e3 + 5;

int n, k, q;
arr<int, N> a, b;

arr<int, N> frq;
void mrg(pii &x, int y) {
    if (y == x.fir) x.sec++;
    if (y > x.fir) x = {y, 1};
}
arr<pii, N> ans;
void upd(int i) {
    int j = (i + k - 1);
    assert(j <= n);

    frq.fill(0);
    for (int x = i; x <= j; x++) frq[a[x]]++;
    for (int x = 1; x <= k; x++) frq[b[x]]++;
    int cnt = 0;
    for (int x = 1; x <= n; x++) cnt += (frq[x] == 2);

    ans[i] = {-1, -1};
    mrg(ans[i], cnt);
    for (int x = 1; x <= n; x++) {
        int y = x + k;
        if (y > n) continue;

        cnt -= (frq[b[x]] == 2);
        frq[b[x]]--, frq[b[y]]++;
        cnt += (frq[b[y]] == 2);
        mrg(ans[i], cnt);
    }
}

map<int, int> nm;
void prnt() { cout << nm.rbegin()->fir << " " << nm.rbegin()->sec << endl; }
void prcmp() {
    for (int i = 1; i <= n; i++) {
        if (i + k - 1 > n) continue;
        upd(i);
        nm[ans[i].fir] += ans[i].sec;
    }
    prnt();
}

signed main() {
    // freopen("a.in", "r", stdin);
    cin >> n >> k >> q;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    
    prcmp();
    for (int i = 1; i <= q; i++) {
        int j; cin >> j;
        int l = j - k + 1, r = j + 1;
        swap(a[j], a[j + 1]);

        if (l >= 1) {
            nm[ans[l].fir] -= ans[l].sec;
            if (nm[ans[l].fir] == 0) nm.erase(ans[l].fir);
            upd(l);
            nm[ans[l].fir] += ans[l].sec;
        }
        if (r + k - 1 <= n) {
            nm[ans[r].fir] -= ans[r].sec;
            if (nm[ans[r].fir] == 0) nm.erase(ans[r].fir);
            upd(r);
            nm[ans[r].fir] += ans[r].sec;
        }
        prnt();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...