题目 › usaco-1522

Election Queries

USACO 2025 US Open · 金组 · 2025-03 · L3 困难 · C++17 · 时限 3 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

**Note: The time limit for this problem is 3s, 1.5x the default.**

Farmer John has $N$ ($2 \leq N \leq 2 \cdot 10^5$) cows numbered from $1$ to $N$. An election is being held in FJ's farm to determine the two new head cows in his farm. Initially, it is known that cow $i$ will vote for cow $a_i$ ($1 \leq a_i \leq N$).

To determine the two head cows, FJ will hold his election in the following process:

  • Choose an arbitrary subset $S$ of his cows that contains at least one cow but not all cows. FJ is able to choose cow $x$ as the first head cow if its votes appear most frequently among all votes cast by cows in $S$.
  • FJ is able to choose cow $y$ as the second head cow if its votes appear most frequently among votes cast by cows not in $S$.
  • For a fixed subset $S$, FJ denotes the diversity between his head cows as $|x - y|$. As FJ does not like having leaders with similar numbers, he wants to choose $S$ such that the diversity is maximized. Note that if FJ is not able to choose two distinct head cows, then the diversity is $0$.

However, some cows keep changing their minds, and FJ may have to rerun the election many times! Therefore, he asks you $Q$ ($1 \leq Q \leq 10^5$) queries. In each query, a cow changes their vote. After each query, he asks you for the maximum possible diversity among his new head cows.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $N$ and $Q$.

The following line contains $a_1, a_2, \ldots, a_N$.

The following $Q$ lines contain two integers $i$ and $x$, representing the update $a_i = x$ ($1 \leq i, x \leq N$).

OUTPUT FORMAT (print output to the terminal / stdout):

Output $Q$ lines, the $i$'th of which is the maximum possible diversity after the first $i$ queries.

SAMPLE INPUT:

5 3
1 2 3 4 5
3 4
1 2
5 2

SAMPLE OUTPUT:

4
3
2

After the first query, $a = [1, 2, 4, 4, 5]$. At the first step of the election, FJ can make $S = \{1, 3\}$. Here, cow $1$ receives one vote and cow $4$ receives one vote. Therefore, FJ can choose either cow $1$ or cow $4$ as its first head cow.

For all cows not in the election, cow $2$ receives one vote, cow $4$ receives one vote, and cow $5$ also receives one vote. Therefore, FJ can choose any one of cows $2$, $4$, or $5$ to be its second head cow.

To obtain the maximum diversity, FJ can choose cow $1$ as the first head cow and cow $5$ as the second head cow. Therefore, the diversity is $|1-5| = 4$.

After the second query, $a=[2,2,4,4,5]$ and FJ can make $S = \{4, 5\}$. Then, he can choose $5$ as the first head cow and cow $2$ as the second head cow. The maximum possible diversity is $|5 - 2| = 3$.

SAMPLE INPUT:

8 5
8 1 4 2 5 4 2 3
7 4
8 4
4 1
5 8
8 4

SAMPLE OUTPUT:

4
4
4
7
7

SCORING:

  • Inputs 3-4: $N, Q \leq 100$
  • Inputs 5-7: $N, Q \leq 3000$
  • Inputs 8-15: No additional constraints.

Problem credits: Chongtian Ma and Haokai Ma

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 6.4k平均输出 token(含思考) ↓
  • 23.1输出速度 tokens/s ↑
  • 187 秒延迟 ↓
  • 0.58×CPU 时间 / 官方解 ↓
  • 同阶 2、更优 1时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 34.2k平均输出 token(含思考) ↓
  • 197.4输出速度 tokens/s ↑
  • 176 秒延迟 ↓
  • 0.53×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

2/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 66.7采样 2 · 答案错误
  • 100采样 3 · 解出
  • 71.9k平均输出 token(含思考) ↓
  • 169.2输出速度 tokens/s ↑
  • 210 秒延迟 ↓
  • 0.15×CPU 时间 / 官方解 ↓
  • 更优 2时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 105 行
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q;
    cin >> N >> Q;

    vector<int> a(N + 1), cnt(N + 1, 0);
    for (int i = 1; i <= N; ++i) {
        cin >> a[i];
        ++cnt[a[i]];
    }

    // buckets[k] contains all cow numbers having exactly k votes.
    vector<set<int>> buckets(N + 1);
    set<int> levels; // Nonempty positive vote counts, in increasing order.

    int maxVotes = 0;
    for (int i = 1; i <= N; ++i) {
        if (cnt[i] > 0) {
            if (buckets[cnt[i]].empty()) levels.insert(cnt[i]);
            buckets[cnt[i]].insert(i);
            maxVotes = max(maxVotes, cnt[i]);
        }
    }

    auto addToBucket = [&](int cow, int votes) {
        if (buckets[votes].empty()) levels.insert(votes);
        buckets[votes].insert(cow);
    };

    auto removeFromBucket = [&](int cow, int votes) {
        buckets[votes].erase(cow);
        if (buckets[votes].empty()) levels.erase(votes);
    };

    vector<int> values, mnPos, mxPos;
    values.reserve(700);
    mnPos.reserve(700);
    mxPos.reserve(700);

    auto computeAnswer = [&]() -> int {
        values.clear();
        mnPos.clear();
        mxPos.clear();

        for (int k : levels) {
            values.push_back(k);
            mnPos.push_back(*buckets[k].begin());
            mxPos.push_back(*buckets[k].rbegin());
        }

        const int d = (int)values.size();
        int p = d;
        int partnerMin = N + 1;
        int partnerMax = -1;
        int answer = 0;

        // A pair with vote counts v and w is possible exactly when v+w >= maxVotes.
        // For each v, maintain all positions whose counts are at least maxVotes-v.
        for (int i = 0; i < d; ++i) {
            int needed = maxVotes - values[i];
            while (p > 0 && values[p - 1] >= needed) {
                --p;
                partnerMin = min(partnerMin, mnPos[p]);
                partnerMax = max(partnerMax, mxPos[p]);
            }

            answer = max(answer, partnerMax - mnPos[i]);
            answer = max(answer, mxPos[i] - partnerMin);
        }

        return answer;
    };

    while (Q--) {
        int i, x;
        cin >> i >> x;

        int old = a[i];
        a[i] = x;

        if (old != x) {
            int oldCount = cnt[old];
            removeFromBucket(old, oldCount);
            --cnt[old];
            if (cnt[old] > 0) addToBucket(old, cnt[old]);

            int targetOldCount = cnt[x];
            if (targetOldCount > 0) removeFromBucket(x, targetOldCount);
            ++cnt[x];
            addToBucket(x, cnt[x]);

            maxVotes = max(maxVotes, cnt[x]);
            while (maxVotes > 0 && buckets[maxVotes].empty()) --maxVotes;
        }

        cout << computeAnswer() << '\n';
    }

    return 0;
}