题目 › usaco-1449

Cowdependence

USACO 2024 December · 金组 · 2024-12 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Farmer John's $N$ $(1 \leq N \leq 10^5)$ cows have been arranged into a line. The $i$th cow has label $a_i$ ($1 \leq a_i \leq N$). A group of cows can form a friendship group if they all have the same label and each cow is within $x$ cows of all the others in the group, where $x$ is an integer in the range $[1,N]$. Every cow must be in exactly one friendship group.

For each $x$ from $1$ to $N$, calculate the minimum number of friendship groups that could have formed.

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

The first line consists of an integer $N$.

The next line contains $a_1 ... a_N$, the labels of each cow.

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

For each $x$ from $1$ to $N$, output the minimum number of friendship groups for that $x$ on a new line.

SAMPLE INPUT:

9
1 1 1 9 2 1 2 1 1

SAMPLE OUTPUT:

7
5
4
4
4
4
4
3
3

Here are examples of how to assign cows to friendship groups for $x=1$ and $x=2$ in a way that minimizes the number of groups. Each letter corresponds to a different group.

Example:

       1 1 1 9 2 1 2 1 1
x = 1: A B B C D E F G G (7 groups)
x = 1: A A B C D E F G G (7 groups, alternative grouping)
x = 2: A A A B C D C E E (5 groups)
x = 2: A A A B C D C D E (5 groups, alternative grouping)

SCORING:

  • Inputs 2-3: $N\le 5000$
  • Inputs 4-7: $a_i\le 10$ for all $i$
  • Inputs 8-11: No label appears more than $10$ times.
  • Inputs 12-20: No additional constraints.

Problem credits: Chongtian Ma

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 44.6k平均输出 token(含思考) ↓
  • 25.3输出速度 tokens/s ↑
  • 1736 秒延迟 ↓
  • 0.31×CPU 时间 / 官方解 ↓
  • 不确定 3时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 47.6k平均输出 token(含思考) ↓
  • 207.4输出速度 tokens/s ↑
  • 244 秒延迟 ↓
  • 0.51×CPU 时间 / 官方解 ↓
  • 不确定 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

2/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 0.0采样 3 · 编译错误
  • 37.2k平均输出 token(含思考) ↓
  • 137.6输出速度 tokens/s ↑
  • 244 秒延迟 ↓
  • 0.24×CPU 时间 / 官方解 ↓
  • 不确定 2时间复杂度(相对官方解)

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

代码

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

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

    int N;
    cin >> N;

    vector<vector<int>> positions(N + 1);
    int distinctLabels = 0;

    for (int i = 0; i < N; ++i) {
        int a;
        cin >> a;
        if (positions[a].empty()) ++distinctLabels;
        positions[a].push_back(i);
    }

    vector<int> multiLabels;
    int singletonLabels = 0;

    for (int label = 1; label <= N; ++label) {
        if (positions[label].size() == 1) {
            ++singletonLabels;
        } else if (positions[label].size() > 1) {
            multiLabels.push_back(label);
        }
    }

    int B = max(1, (int)sqrt((double)N));
    int K = (N - 1) / (B + 1) + 1;

    vector<int> answer(N + 1, 0);
    vector<int> delta(N + 2, 0);

    // Directly compute the answers for small x.
    for (int x = 1; x <= B; ++x) {
        int total = singletonLabels;

        for (int label : multiLabels) {
            const vector<int> &v = positions[label];
            int m = (int)v.size();
            int j = 0;

            while (j < m) {
                ++total;
                int start = v[j++];
                while (j < m && v[j] - start <= x) ++j;
            }
        }

        answer[x] = total;
    }

    const int firstLargeX = B + 1;

    // A threshold g contributes one extra group for firstLargeX <= x < g.
    auto addThreshold = [&](int g) {
        if (g > firstLargeX) {
            ++delta[firstLargeX];
            --delta[g];
        }
    };

    /*
     * For each label, let g[k] be the smallest x allowing at most k groups.
     * For large x, only g[1], ..., g[min(m,K)-1] are needed.
     *
     * DP transition, covering the first i occurrences with k groups:
     * dp[i] = min over j of max(previous[j], v[i-1] - v[j]).
     * The monotone pointer finds the best j in amortized O(1).
     */
    for (int label : multiLabels) {
        const vector<int> &v = positions[label];
        int m = (int)v.size();
        int limit = min(m, K);

        if (limit <= 1) continue;

        int g = v.back() - v.front();
        addThreshold(g);

        if (g <= firstLargeX || limit == 2) continue;

        vector<int> previous(m + 1), current(m + 1);

        // DP for one group.
        for (int i = 1; i <= m; ++i) {
            previous[i] = v[i - 1] - v[0];
        }

        for (int k = 2; k < limit; ++k) {
            int ptr = k - 1;

            for (int i = k; i <= m; ++i) {
                int endpoint = v[i - 1];

                while (ptr < i - 1 &&
                       previous[ptr] + v[ptr] < endpoint) {
                    ++ptr;
                }

                int best = previous[ptr];
                if (ptr > k - 1) {
                    best = min(best, endpoint - v[ptr - 1]);
                }

                current[i] = best;
            }

            g = current[m];
            addThreshold(g);

            if (g <= firstLargeX) break;
            swap(previous, current);
        }
    }

    int extraGroups = 0;
    for (int x = firstLargeX; x <= N; ++x) {
        extraGroups += delta[x];
        answer[x] = distinctLabels + extraGroups;
    }

    for (int x = 1; x <= N; ++x) {
        cout << answer[x] << '\n';
    }

    return 0;
}