题目 › usaco-1473

Median Heap

USACO 2025 January · 金组 · 2025-01 · L3 困难 · C++17 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

**Note: The time limit for this problem is 4s, twice the default.**

Farmer John has a binary tree with $N$ nodes where the nodes are numbered from $1$ to $N$ ($1 \leq N < 2\cdot 10^5$ and $N$ is odd). For $i>1$, the parent of node $i$ is $\lfloor i/2\rfloor$. Each node has an initial integer value $a_i$, and a cost $c_i$ to change the initial value to any other integer value ($0\le a_i,c_i\le 10^9$).

He has been tasked by the Federal Bovine Intermediary (FBI) with finding an approximate median value within this tree, and has devised a clever algorithm to do so.

He starts at the last node $N$ and works his way backward. At every step of the algorithm, if a node would not be the median of it and its two children, he swaps the values of the current node and the child value that would be the median. At the end of this algorithm, the value at node $1$ (the root) is the median approximation.

The FBI has also given Farmer John a list of $Q$ $(1 \leq Q \leq 2\cdot 10^5)$ independent queries each specified by a target value $m$ ($0\le m\le 10^9$). For each query, FJ will first change some of the node's initial values, and then execute the median approximation algorithm. For each query, determine the minimum possible total cost for FJ to make the output of the algorithm equal to $m$.

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

The first line of input contains $N$.

The next $N$ lines each contain two integers $a_i$ and $c_i$.

The next line contains $Q$.

The next $Q$ lines each contain a target value $m$.

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

Output $Q$ lines, the minimum possible total cost for each target value $m$.

SAMPLE INPUT:

5
10 10000
30 1000
20 100
50 10
40 1
11
55
50
45
40
35
30
25
20
15
10
5

SAMPLE OUTPUT:

111
101
101
100
100
100
100
0
11
11
111

To make the median approximation equal $40$, FJ can change the value at node 3 to $60$. This costs $c_3=100$.

To make the median approximation equal $45$, FJ can change the value at node 3 to $60$ and the value at node 5 to $45$. This costs $c_3+c_5=100+1=101$.

SCORING:

  • Inputs 2-4: $N,Q\le 50$
  • Inputs 5-7: $N,Q\le 1000$
  • Inputs 8-16: No additional constraints

Problem credits: Suhas Nagar and Benjamin Qi

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 21.4k平均输出 token(含思考) ↓
  • 23.2输出速度 tokens/s ↑
  • 900 秒延迟 ↓
  • 0.15×CPU 时间 / 官方解 ↓
  • 更优 3时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

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

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

代码

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

using int64 = long long;

static constexpr int LIM = 1000000000;

struct Entry {
    int start;
    array<int64, 3> dp; // minimum costs for output < m, = m, > m
};

struct Function {
    vector<Entry> seg;
};

int N;
vector<int> a;
vector<int64> c;

inline int64 min3(int64 x, int64 y, int64 z) {
    return min(x, min(y, z));
}

void addSegment(Function &f, int start, const array<int64, 3> &dp) {
    if (!f.seg.empty()) {
        const auto &last = f.seg.back().dp;
        if (last[0] == dp[0] && last[1] == dp[1] && last[2] == dp[2]) {
            return;
        }
    }
    f.seg.push_back({start, dp});
}

Function makeLeaf(int value, int64 cost) {
    Function f;
    f.seg.reserve(3);

    if (value > 0) {
        addSegment(f, 0, {cost, cost, 0});
    }
    addSegment(f, value, {cost, 0, cost});
    if (value < LIM) {
        addSegment(f, value + 1, {0, cost, cost});
    }
    return f;
}

Function combine(const Function &left, const Function &right,
                 int value, int64 cost) {
    Function result;
    result.seg.reserve(left.seg.size() + right.seg.size() + 2);

    size_t p = 0, q = 0;
    int pos = 0;

    while (pos <= LIM) {
        while (p + 1 < left.seg.size() &&
               left.seg[p + 1].start <= pos) {
            ++p;
        }
        while (q + 1 < right.seg.size() &&
               right.seg[q + 1].start <= pos) {
            ++q;
        }

        const auto &y = left.seg[p].dp;
        const auto &z = right.seg[q].dp;

        int next = LIM + 1;
        if (p + 1 < left.seg.size()) {
            next = min(next, left.seg[p + 1].start);
        }
        if (q + 1 < right.seg.size()) {
            next = min(next, right.seg[q + 1].start);
        }

        array<int64, 3> x;
        if (pos < value) {
            x = {cost, cost, 0};
            next = min(next, value);
        } else if (pos == value) {
            x = {cost, 0, cost};
            next = min(next, value + 1);
        } else {
            x = {0, cost, cost};
        }

        int64 minY = min3(y[0], y[1], y[2]);
        int64 minZ = min3(z[0], z[1], z[2]);

        // The median is below m exactly when at least two inputs are below m.
        int64 below = min3(
            y[0] + z[0],
            x[0] + y[0] + minZ,
            x[0] + z[0] + minY
        );

        // Similarly for the median being above m.
        int64 above = min3(
            y[2] + z[2],
            x[2] + y[2] + minZ,
            x[2] + z[2] + minY
        );

        // Either at least two inputs equal m, or the inputs are
        // below m, equal to m, and above m in some order.
        int64 equalPair = min3(
            y[1] + z[1],
            x[1] + y[1] + minZ,
            x[1] + z[1] + minY
        );

        int64 mixed = min3(
            x[0] + min(y[1] + z[2], y[2] + z[1]),
            x[1] + min(y[0] + z[2], y[2] + z[0]),
            x[2] + min(y[0] + z[1], y[1] + z[0])
        );

        int64 equal = min(equalPair, mixed);
        addSegment(result, pos, {below, equal, above});

        pos = next;
    }

    return result;
}

Function build(int u) {
    if (2 * u > N) {
        return makeLeaf(a[u], c[u]);
    }

    Function left = build(2 * u);
    Function right = build(2 * u + 1);
    return combine(left, right, a[u], c[u]);
}

int64 query(const Function &f, int m) {
    int lo = 0, hi = static_cast<int>(f.seg.size()) - 1;
    while (lo < hi) {
        int mid = (lo + hi + 1) / 2;
        if (f.seg[mid].start <= m) {
            lo = mid;
        } else {
            hi = mid - 1;
        }
    }
    return f.seg[lo].dp[1];
}

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

    cin >> N;
    a.resize(N + 1);
    c.resize(N + 1);

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

    Function root = build(1);

    int Q;
    cin >> Q;
    while (Q--) {
        int m;
        cin >> m;
        cout << query(root, m) << '\n';
    }

    return 0;
}