题目 › usaco-1497

Bessie's Function

USACO 2025 February · 金组 · 2025-02 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie has a special function $f(x)$ that takes as input an integer in $[1, N]$ and returns an integer in $[1, N]$ ($1 \le N \le 2 \cdot 10^5$). Her function $f(x)$ is defined by $N$ integers $a_1 \ldots a_N$ where $f(x) = a_x$ ($1 \le a_i \le N$).

Bessie wants this function to be idempotent. In other words, it should satisfy $f(f(x)) = f(x)$ for all integers $x \in [1, N]$.

For a cost of $c_i$, Bessie can change the value of $a_i$ to any integer in $[1, N]$ ($1 \le c_i \le 10^9$). Determine the minimum total cost Bessie needs to make $f(x)$ idempotent.

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

The first line contains $N$.

The second line contains $N$ space-separated integers $a_1,a_2,\dots,a_N$.

The third line contains $N$ space-separated integers $c_1,c_2,\dots,c_N$.

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

Output the minimum total cost Bessie needs to make $f(x)$ idempotent.

SAMPLE INPUT:

5
2 4 4 5 3
1 1 1 1 1

SAMPLE OUTPUT:

3

We can change $a_1 = 4$, $a_4 = 4$, $a_5 = 4$. Since all $c_i$ equal one, the total cost is equal to $3$, the number of changes. It can be shown that there is no solution with only $2$ or fewer changes.

SAMPLE INPUT:

8
1 2 5 5 3 3 4 4
9 9 2 5 9 9 9 9

SAMPLE OUTPUT:

7

We change $a_3 = 3$ and $a_4 = 4$. The total cost is $2+5=7$.

SCORING:

Subtasks:

  • Input 3: $N\le 20$
  • Inputs 4-9: $a_i\ge i$
  • Inputs 10-15: All $a_i$ are distinct.
  • Inputs 16-21: No additional constraints.

Additionally, in each of the last three subtasks, the first half of tests will satisfy $c_i=1$ for all $i$.

Problem credits: Avnith Vijayram

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 22.4k平均输出 token(含思考) ↓
  • 216.3输出速度 tokens/s ↑
  • 99 秒延迟 ↓
  • 0.40×CPU 时间 / 官方解 ↓
  • 不可测 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

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

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

代码

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

using int64 = long long;

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

    int N;
    cin >> N;

    vector<int> a(N + 1);
    vector<int64> c(N + 1);

    for (int i = 1; i <= N; ++i) cin >> a[i];
    for (int i = 1; i <= N; ++i) cin >> c[i];

    vector<int> indegree(N + 1, 0);
    int64 totalCost = 0;

    for (int i = 1; i <= N; ++i) {
        ++indegree[a[i]];
        if (a[i] != i) totalCost += c[i];
    }

    // dp0[v]: maximum savings in v's tree subtree if v is not fixed.
    // dp1[v]: same, if v is fixed. The edge from v to its parent is excluded.
    vector<int64> dp0(N + 1, 0), dp1(N + 1, 0);
    vector<char> removed(N + 1, false);

    queue<int> q;
    for (int i = 1; i <= N; ++i) {
        if (indegree[i] == 0) q.push(i);
    }

    // Peel off all non-cycle vertices, accumulating tree DP values.
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        removed[v] = true;

        int p = a[v];
        dp0[p] += max(dp0[v], dp1[v]);
        dp1[p] += max(dp1[v], dp0[v] + c[v]);

        if (--indegree[p] == 0) q.push(p);
    }

    const int64 NEG = -(1LL << 60);
    vector<char> seen(N + 1, false);
    int64 totalSavings = 0;

    auto baseValue = [&](int v, int state) -> int64 {
        return state == 0 ? dp0[v] : dp1[v];
    };

    // Solve the remaining cycle of each component.
    for (int i = 1; i <= N; ++i) {
        if (removed[i] || seen[i]) continue;

        vector<int> cycle;
        int v = i;
        while (!seen[v]) {
            seen[v] = true;
            cycle.push_back(v);
            v = a[v];
        }

        int L = (int)cycle.size();

        if (L == 1) {
            int u = cycle[0];
            totalSavings += max(dp0[u], dp1[u]);
            continue;
        }

        // Vertices are in directed order: cycle[k] -> cycle[k+1].
        // An edge saves its cost exactly when its tail is not fixed and
        // its head is fixed.
        int64 best = NEG;

        for (int firstState = 0; firstState < 2; ++firstState) {
            int64 cur[2] = {NEG, NEG};
            cur[firstState] = baseValue(cycle[0], firstState);

            for (int k = 1; k < L; ++k) {
                int64 nxt[2] = {NEG, NEG};

                for (int prevState = 0; prevState < 2; ++prevState) {
                    if (cur[prevState] == NEG) continue;

                    for (int state = 0; state < 2; ++state) {
                        int64 value = cur[prevState]
                                    + baseValue(cycle[k], state);

                        if (prevState == 0 && state == 1) {
                            value += c[cycle[k - 1]];
                        }

                        nxt[state] = max(nxt[state], value);
                    }
                }

                cur[0] = nxt[0];
                cur[1] = nxt[1];
            }

            for (int lastState = 0; lastState < 2; ++lastState) {
                int64 value = cur[lastState];
                if (lastState == 0 && firstState == 1) {
                    value += c[cycle.back()];
                }
                best = max(best, value);
            }
        }

        totalSavings += best;
    }

    cout << totalCost - totalSavings << '\n';
    return 0;
}