题目 › usaco-1477

Shock Wave

USACO 2025 January · 铂金组 · 2025-01 · L4 顶尖 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie is experimenting with a powerful hoof implant that has the ability to create massive shock waves. She has $N$ ($2 \leq N \leq 10^5$) tiles lined up in front of her, which require powers of at least $p_0,p_1,\dots,p_{N-1}$ to break, respectively ($0 \leq p_i \leq 10^{18}$).

Bessie can apply power by punching a specific tile but due to the strange nature of her implant, it will not apply any power to the tile she punches. Instead, if she chooses to punch tile $x$ once, where $x$ is an integer in $[0,N-1]$, it applies $|i-x|$ power to tile $i$ for all integers $i$ in the range $[0,N-1]$. This power is also cumulative, so applying $2$ power twice to a tile will apply a total of $4$ power to the tile.

Please determine the fewest number of punches required to break all the tiles.

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

The first line contains $T$ ($1 \leq T \leq 100$) representing the number of test cases.

Line $2t$ contains a single integer $N$, the number of tiles in test case $t$.

Line $2t+1$ contains $N$ space separated numbers $p_0,p_1, \ldots, p_{N-1}$ representing that tile $i$ takes $p_i$ power to be broken.

It is guaranteed that the sum of all $N$ in a single input does not exceed $5\cdot 10^5$.

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

$T$ lines, the $i$th line representing the answer to the $i$th test case.

SAMPLE INPUT:

6
5
0 2 4 5 8
5
6 5 4 5 6
5
1 1 1 1 1
5
12 10 8 6 4
7
6 1 2 3 5 8 13
2
1000000000000000000 1000000000000000000

SAMPLE OUTPUT:

2
3
2
4
4
2000000000000000000

For the first test, the only way for Bessie to break all the tiles with two punches is to punch $0$ twice, applying total powers of $[0,2,4,6,8]$ respectively.

For the second test, one way for Bessie to break all the tiles with three punches is to punch $0$, $2$, and $4$ one time each, applying total powers of $[6,5,4,5,6]$ respectively.

For the third test, one way for Bessie to break all the tiles with two punches is to punch $0$ and $1$ one time each, applying total powers of $[1,1,3,5,7]$ respectively.

SCORING:

  • Input 2: All $p_i$ are equal.
  • Inputs 3-6: $N\le 100$
  • Inputs 7-14: No additional constraints.

Problem credits: Suhas Nagar

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

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

Kimi K3.1 Pro(high)Kimi high

high

1/3

解出的采样 ↑

  • 14.3采样 1 · 答案错误
  • 100采样 2 · 解出
  • 14.3采样 3 · 答案错误
  • 34.4k平均输出 token(含思考) ↓
  • 21.5输出速度 tokens/s ↑
  • 1549 秒延迟 ↓
  • 1.24×CPU 时间 / 官方解 ↓
  • 同阶 1时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 14.3采样 2 · 答案错误
  • 7.1采样 3 · 答案错误
  • 123.0k平均输出 token(含思考) ↓
  • 185.1输出速度 tokens/s ↑
  • 660 秒延迟 ↓
  • 9.33×CPU 时间 / 官方解 ↓
  • 同阶 1时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

1/3

解出的采样 ↑

  • 0.0采样 1 · 编译错误
  • 100采样 2 · 解出
  • 7.1采样 3 · 答案错误
  • 87.0k平均输出 token(含思考) ↓
  • 111.9输出速度 tokens/s ↑
  • 733 秒延迟 ↓
  • 3.82×CPU 时间 / 官方解 ↓
  • 同阶 1时间复杂度(相对官方解)

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

代码

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

using int64 = long long;
using i128 = __int128_t;

struct Rat {
    i128 n;
    int64 d;
};

struct Interval {
    bool ok;
    Rat lo, hi;
};

// Interval of real a for which a punches at 0 and K-a punches at N-1
// (fractional punches allowed) cover all requirements.
Interval getInterval(int64 K, const vector<int64>& p) {
    const int N = (int)p.size();
    const int L = N - 1;

    Interval res{true, {0, 1}, {(i128)K, 1}};

    for (int i = 0; i < N; ++i) {
        i128 base = (i128)K * (L - i);
        int64 d = 2LL * i - L;

        if (d == 0) {
            if (base < (i128)p[i]) {
                res.ok = false;
                return res;
            }
        } else if (d > 0) {
            i128 num = (i128)p[i] - base;
            if (num > 0 && num * res.lo.d > res.lo.n * d) {
                res.lo = {num, d};
            }
        } else {
            int64 e = -d;
            i128 num = base - (i128)p[i];
            if (num < 0) {
                res.ok = false;
                return res;
            }
            if (num * res.hi.d < res.hi.n * e) {
                res.hi = {num, e};
            }
        }
    }

    res.ok = (res.lo.n * res.hi.d <= res.hi.n * res.lo.d);
    return res;
}

// Exact feasibility with K integral punches.
bool integerFeasible(int64 K, const vector<int64>& p) {
    const int N = (int)p.size();
    const int L = N - 1;

    Interval in = getInterval(K, p);
    if (!in.ok) return false;

    i128 ceilLo = in.lo.n / in.lo.d;
    if (in.lo.n % in.lo.d != 0) ++ceilLo;
    i128 floorHi = in.hi.n / in.hi.d;

    // Integral endpoint-only solution.
    if (ceilLo <= floorHi) return true;

    // Otherwise only one possible pair of endpoint base counts remains.
    i128 q = in.lo.n / in.lo.d;
    if (q < 0 || q >= (i128)K) return false;

    int64 A = (int64)q;
    int64 B = K - 1 - A;

    // Check A punches at 0, B punches at N-1, and one additional punch x.
    vector<i128> r(N), suffix(N);
    for (int i = 0; i < N; ++i) {
        i128 base = (i128)A * i + (i128)B * (L - i);
        r[i] = (i128)p[i] - base;
    }

    const i128 INF = i128(1) << 120;
    i128 mn = INF;
    for (int i = N - 1; i >= 0; --i) {
        mn = min(mn, (i128)i - r[i]);
        suffix[i] = mn;
    }

    i128 mx = -INF;
    for (int x = 0; x < N; ++x) {
        mx = max(mx, (i128)x + r[x]);
        if (mx <= (i128)x && (i128)x <= suffix[x]) return true;
    }
    return false;
}

// Ceiling of the fractional (linear programming) optimum.
int64 fractionalCeil(const vector<int64>& p) {
    const int N = (int)p.size();
    const int L = N - 1;

    vector<int> hull;
    hull.reserve(N);

    // Upper concave hull of (i,p_i).
    for (int i = 0; i < N; ++i) {
        while ((int)hull.size() >= 2) {
            int a = hull[(int)hull.size() - 2];
            int b = hull.back();
            i128 lhs = ((i128)p[b] - p[a]) * (i - b);
            i128 rhs = ((i128)p[i] - p[b]) * (b - a);
            if (lhs <= rhs) hull.pop_back();
            else break;
        }
        hull.push_back(i);
    }

    // Evaluate the hull at x=L/2.
    int e = 0;
    while (e + 1 < (int)hull.size() && 2LL * hull[e + 1] < L) ++e;

    int x1 = hull[e], x2 = hull[e + 1];
    int64 dx = x2 - x1;

    i128 den = (i128)L * dx;
    i128 num = 2 * (i128)p[x1] * dx
             + ((i128)p[x2] - p[x1]) * (L - 2LL * x1);

    if (num <= 0) return 0;
    return (int64)((num + den - 1) / den);
}

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

    int T;
    cin >> T;

    while (T--) {
        int N;
        cin >> N;
        vector<int64> p(N);
        for (auto &x : p) cin >> x;

        int64 m = fractionalCeil(p);
        int64 answer = integerFeasible(m, p) ? m : m + 1;
        cout << answer << '\n';
    }

    return 0;
}