比赛 26暑假集训模拟赛3 评测结果 AAAATTTTTTTTTTT
题目名称 Election Queries 最终得分 24
用户昵称 RpUtl 运行时间 34.126 s
代码语言 C++ 内存使用 4.26 MiB
提交时间 2026-07-06 11:29:52
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10; 
int n, q, a[N], c[N], ans;
int main() {
    freopen("Queries.in", "r", stdin);
    freopen("Queries.out", "w", stdout);
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        c[a[i]]++;
    }
    ans = 0;
    int x, y, u, v;
    while (q--) {
        cin >> x >> y;
        c[a[x]]--, c[a[x] = y]++;
        ans = 0;
        int mx = 0, res = -1, i, pos;
        for (int i = 1; i <= n; i++) {
            mx = max(mx, c[i]);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (c[i] && c[j] && c[i] + c[j] >= mx) {
                    res = max(res, abs(i - j));
                }
            }
        }
        cout << res << '\n';
    }
    return 0;
}