题目 › usaco-1546

Milk Buckets

USACO 2026 First · 金组 · 2026-01 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie has challenged Farmer John to a game involving milk buckets! There are $N$ $(2 \leq N \leq 2 \cdot 10^5)$ milk buckets lined up in a row. The $i$-th bucket from the left initially contains $a_i$ $(0 \leq a_i \leq 10^9)$ gallons of milk.

The game consists of two phases:

Phase 1: Farmer John may swap any two adjacent buckets. He may perform as many swaps as he likes, but each swap costs 1 coin.

Phase 2: After swapping, Farmer John performs the following operation until only one bucket is left: Choose two adjacent buckets with milk amounts $a_i$ and $a_{i+1}$, and replace both buckets with one bucket containing $\frac{a_i+a_{i+1}}2$ gallons of milk in their place.

Your goal is to determine the minimum number of coins Farmer John must spend in the swapping phase to maximize the amount of milk in the final bucket after all merges are complete.

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

The first line contains one integer $T$ $(1 \leq T \leq 100)$: the number of independent test cases.

Then, for each test case, the first line contains an integer $N$: the number of milk buckets. The second line contains $N$ integers $a_1, a_2, \dots, a_N$, separated by spaces: the number of gallons of milk in each bucket.

It is guaranteed that the sum of $N$ over all test cases does not exceed $5 \cdot 10^5.$

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

For each test case, output the minimum number of coins Farmer John must spend to maximize the amount of milk in the final bucket.

SAMPLE INPUT:

2
3
0 0 1
3
0 1 0

SAMPLE OUTPUT:

0
1

For the first test, we do not need to swap any milk buckets in the first phase. In the second phase, Farmer John can merge the first two buckets and then merge the only two buckets left to achieve a final amount of 0.5. It can be shown that this final amount is maximal.

For the second test, we must perform a singular swap of the first two milk buckets in the first stage to achieve a final amount of 0.5 in the second stage. It can be shown that we cannot achieve a final amount of 0.5 without swaps in the first stage.

SAMPLE INPUT:

4
4
9 4 9 2
6
0 0 2 0 0 0
3
2 0 1
9
3 3 3 10 3 2 13 14 13

SAMPLE OUTPUT:

1
2
0
3

For the first test, Farmer John can swap the second and the third buckets in the first phase. Then, in the second phase, Farmer John can perform the following:

  • $[9,9,4,2]$ -> merge the third and fourth buckets ->
  • $[9,9,3]$ -> merge the second and third buckets ->
  • $[9,6]$ -> merge the first and second buckets ->
  • $[7.5]$

The final amount of milk is 7.5, which is the maximum possible. It can be shown that even with additional swaps, the final amount cannot exceed 7.5, and that with fewer swaps, the final amount cannot reach 7.5.

SCORING:

  • Inputs 3-4: $a_i\le 1$ and $N\le 2000$ (sum of $N\le 5000$)
  • Inputs 5-6: $a_i\le 1$
  • Inputs 7-9: $N\le 2000$ (sum of $N\le 5000$)
  • Inputs 10-14: No additional constraints.

Problem credits: Charlie Yang

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

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

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

代码

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

struct Fenwick {
    int n;
    vector<int> bit;
    Fenwick(int n_) : n(n_), bit(n_ + 1, 0) {}

    void add(int idx, int val) {
        for (; idx <= n; idx += idx & -idx) bit[idx] += val;
    }

    int sum(int idx) const {
        int res = 0;
        for (; idx > 0; idx -= idx & -idx) res += bit[idx];
        return res;
    }
};

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

    int T;
    cin >> T;

    while (T--) {
        int N;
        cin >> N;

        vector<int> a(N);
        vector<int> vals;
        vals.reserve(N);

        for (int &x : a) {
            cin >> x;
            vals.push_back(x);
        }

        sort(vals.begin(), vals.end());
        vals.erase(unique(vals.begin(), vals.end()), vals.end());

        int M = vals.size();
        vector<int> rank(N), freq(M, 0);

        for (int i = 0; i < N; i++) {
            rank[i] = lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin();
            freq[rank[i]]++;
        }

        vector<int> totalSmaller(M);
        int prefix = 0;
        for (int i = 0; i < M; i++) {
            totalSmaller[i] = prefix;
            prefix += freq[i];
        }

        Fenwick fw(M);
        long long answer = 0;

        for (int i = 0; i < N; i++) {
            int r = rank[i];
            int leftSmaller = fw.sum(r); // ranks 0 .. r-1 are strictly smaller
            int rightSmaller = totalSmaller[r] - leftSmaller;
            answer += min(leftSmaller, rightSmaller);
            fw.add(r + 1, 1);
        }

        cout << answer << '\n';
    }

    return 0;
}