题目 › usaco-1569

Balancing the Barns

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

查看原题跳到结果

题面

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

Farmer John has $N$ ($1\le N\le 5\cdot 10^4$) barns arranged along a road. The $i$-th barn contains $a_i$ bales of hay and $b_i$ bags of feed $(0\le a_i,b_i\le 10^9$).

Bessie has been complaining about the inequality between barns. She defines the "imbalance" of the farm as the difference between the maximum hay in any barn and the minimum feed in any barn. Formally, the imbalance is $\max(a) - \min(b)$.

To address Bessie's concerns, Farmer John can perform exactly $K$ ($1\le K\le 10^{18}$) transfers. In each transfer, he selects a barn $i$, sells one of its haybales, and buys it a new bag of feed for the same barn. Note that there can be negative amounts in his farm (he is not afraid of debt). Formally, $K$ times, you choose an index $i\in [1,N]$, decrement $a_i$, and increment $b_i$.

Help Farmer John determine the minimum possible imbalance after performing exactly $K$ transfers.

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

The first line contains $T$ ($1 \leq T \leq 10^3$), the number of independent test cases.

The first line of each test case contains $N$ and $K$.

The following line contains $a_1\dots a_N$.

The following line contains $b_1\dots b_N$.

The sum of $N$ over all test cases is at most $5 \cdot 10^4$.

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

For each test case, output a single integer, the minimum possible value of $\max(a) - \min(b)$ after performing $K$ operations.

SAMPLE INPUT:

4
1 10
5
3
2 6
100 96
0 4
3 3
1 1 2
0 0 1
3 3
1 2 2
0 1 1

SAMPLE OUTPUT:

-18
90
0
0

In the first test case, Farmer John can transfer $10$ haybales from barn $1$ into bags of feed. This leaves $a = [-5]$ and $b = [13]$. The imbalance is $\max(a) - \min(b) = -5 - 13 = -18$.

In the second test case, Farmer john can transfer $5$ haybales from barn $1$ and $1$ haybale from barn $2$. This leaves $a = [95, 95]$ and $b = [5, 5]$. The imbalance is $95 - 5 = 90$. This is the minimum imbalance Farmer John can achieve.

SCORING:

  • Inputs 2-4: $K\le 500$, sum of $N$ over all testcases is $\le 500$
  • Inputs 5-8: Sum of $N$ over all testcases is $\le 500$
  • Inputs 9-13: No additional constraints.

Problem credits: Rohin Garg

结果

Kimi K3.1 ProKimi

auto(max)

2/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

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

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

代码

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

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

    int T;
    cin >> T;

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

        long long a, b;
        cin >> a >> b;

        bool extraHay = false;
        for (int i = 3; i <= N; i++) {
            long long x;
            cin >> x;
            if (x > 0) extraHay = true;
        }

        // If a+b is even, one transfer between barns 1 and 2 equalizes them.
        // If it is odd, adding one haybale from any other nonempty barn makes
        // the total even. Otherwise, the odd total forces imbalance at least 1.
        if (((a + b) & 1LL) && !extraHay)
            cout << 1 << '\n';
        else
            cout << 0 << '\n';
    }

    return 0;
}