题目 › usaco-1500

Min Max Subarrays

USACO 2025 February · 铂金组 · 2025-02 · L4 顶尖 · C++17 · 时限 3 秒 · 测试点等分

查看原题跳到结果

题面

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

**Note: The time limit for this problem is 3s, 1.5x the default.**

You are given a length-$N$ integer array $a_1,a_2,\dots,a_N$ ($2\le N\le 10^6, 1\le a_i\le N$). Output the sum of the answers for the subproblem below over all $N(N+1)/2$ contiguous subarrays of $a$.

Given a nonempty list of integers, alternate the following operations (starting with the first operation) until the list has size exactly one.

  1. Replace two consecutive integers in the list with their minimum.
  2. Replace two consecutive integers in the list with their maximum.

Determine the maximum possible value of the final remaining integer.

For example,

[4, 10, 3] -> [4, 3] -> [4]
[3, 4, 10] -> [3, 10] -> [10]

In the first array, $(10, 3)$ is replaced by $\min(10, 3)=3$ and $(4, 3)$ is replaced by $\max(4, 3)=4$.

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

The first line contains $N$.

The second line contains $a_1,a_2,\dots,a_N$.

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

The sum of the answer to the subproblem over all subarrays.

SAMPLE INPUT:

2
2 1

SAMPLE OUTPUT:

4

The answer for $[2]$ is $2$, the answer for $[1]$ is $1$, and the answer for $[2, 1]$ is $1$.

Thus, our output should be $2+1+1 = 4$.

SAMPLE INPUT:

3
3 1 3

SAMPLE OUTPUT:

12

SAMPLE INPUT:

4
2 4 1 3

SAMPLE OUTPUT:

22

Consider the subarray $[2, 4, 1, 3]$.

  1. Applying the first operation on (1, 3), our new array is $[2, 4, 1]$.
  2. Applying the second operation on (4, 1), our new array is $[2, 4]$.
  3. Applying the third operation on (2, 4), our final number is $2$.

It can be proven that $2$ is the maximum possible value of the final number.

SCORING:

  • Inputs 4-5: $N\le 100$
  • Inputs 6-7: $N\le 5000$
  • Inputs 8-9: $\max(a)\le 10$
  • Inputs 10-13: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

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

Kimi K3.1 Pro(high)Kimi high

high

1/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

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

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

代码

Kimi K3.1 Pro · 采样 1 · 158 行
#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);
    int maxValue = 0;
    for (int i = 1; i <= N; ++i) {
        cin >> a[i];
        maxValue = max(maxValue, a[i]);
    }

    // Positions are bucketed by value.
    vector<int> head(maxValue + 1, -1), nextInBucket(N + 1, -1);
    for (int i = 1; i <= N; ++i) {
        nextInBucket[i] = head[a[i]];
        head[a[i]] = i;
    }

    auto gapEven = [](int g) -> int64 {
        return 1LL * (g / 2) * ((g + 1) / 2);
    };

    auto gapOdd = [&](int g) -> int64 {
        return 1LL * g * (g + 1) / 2 - gapEven(g);
    };

    // Number of even-length subarrays containing p, with left endpoint in
    // [P+1,p] and right endpoint in [p,Q-1].
    auto oneActiveEven = [](int p, int P, int Q) -> int64 {
        int64 leftEven = p / 2 - P / 2;
        int64 leftOdd = (p + 1) / 2 - (P + 1) / 2;
        int64 rightEven = (Q - 1) / 2 - (p - 1) / 2;
        int64 rightOdd = Q / 2 - p / 2;
        return leftEven * rightOdd + leftOdd * rightEven;
    };

    const int64 totalSubarrays = 1LL * N * (N + 1) / 2;
    const int64 totalEven = gapEven(N);
    const int64 totalOdd = totalSubarrays - totalEven;

    // Initially every position is active.
    vector<int> left(N + 2), right(N + 2);
    for (int i = 1; i <= N; ++i) {
        left[i] = i - 1;
        right[i] = i + 1;
    }
    right[0] = 1;
    left[N + 1] = N;

    int64 zeroEven = 0, zeroOdd = 0, exactlyOneEven = 0;
    int64 sumMaximumOdd = 0, sumSecondMaximumEven = 0;
    int previousValue = 0;

    // Process thresholds increasingly, removing positions below the next threshold.
    for (int value = 1; value <= maxValue; ++value) {
        if (head[value] == -1) continue;

        int64 withActiveOdd = totalOdd - zeroOdd;
        int64 withTwoActiveEven = totalEven - zeroEven - exactlyOneEven;

        int64 delta = value - previousValue;
        sumMaximumOdd += delta * withActiveOdd;
        sumSecondMaximumEven += delta * withTwoActiveEven;
        previousValue = value;

        for (int p = head[value]; p != -1; p = nextInBucket[p]) {
            int P = left[p], Q = right[p];

            exactlyOneEven -= oneActiveEven(p, P, Q);

            if (P != 0) {
                int PP = left[P];
                exactlyOneEven -= oneActiveEven(P, PP, p);
                exactlyOneEven += oneActiveEven(P, PP, Q);
            }
            if (Q != N + 1) {
                int QQ = right[Q];
                exactlyOneEven -= oneActiveEven(Q, p, QQ);
                exactlyOneEven += oneActiveEven(Q, P, QQ);
            }

            int oldGapLeft = p - P - 1;
            int oldGapRight = Q - p - 1;
            int mergedGap = Q - P - 1;

            zeroEven += gapEven(mergedGap)
                      - gapEven(oldGapLeft) - gapEven(oldGapRight);
            zeroOdd += gapOdd(mergedGap)
                     - gapOdd(oldGapLeft) - gapOdd(oldGapRight);

            right[P] = Q;
            left[Q] = P;
        }
    }

    /*
       For odd lengths at least 5, the answer is the subarray maximum.
       For even lengths at least 8, it is the second maximum.
       Lengths 1 and 2 already agree with these formulas.
       Only lengths 3, 4, and 6 need corrections.
    */
    int64 correction = 0;

    for (int i = 1; i + 2 <= N; ++i) {
        int x = a[i], y = a[i + 1], z = a[i + 2];
        int rangeMaximum = max(x, max(y, z));
        int answer = max(max(min(x, y), z), max(x, min(y, z)));
        correction += answer - rangeMaximum;
    }

    for (int i = 1; i + 3 <= N; ++i) {
        int v[4] = {a[i], a[i + 1], a[i + 2], a[i + 3]};
        sort(v, v + 4);
        int second = v[2], third = v[1];

        if (second > third) {
            int positions[2], count = 0;
            for (int j = 0; j < 4; ++j) {
                if (a[i + j] >= second) positions[count++] = j + 1;
            }
            int p = positions[0], q = positions[1];
            bool losing = (p == 1 && q == 3)
                       || (p == 2 && q == 3)
                       || (p == 2 && q == 4);
            if (losing) correction += (int64)third - second;
        }
    }

    for (int i = 1; i + 5 <= N; ++i) {
        int v[6] = {a[i], a[i + 1], a[i + 2],
                    a[i + 3], a[i + 4], a[i + 5]};
        sort(v, v + 6);
        int second = v[4], third = v[3];

        if (second > third) {
            int positions[2], count = 0;
            for (int j = 0; j < 6; ++j) {
                if (a[i + j] >= second) positions[count++] = j + 1;
            }
            int p = positions[0], q = positions[1];
            bool losing = (p == 2 && q == 4)
                       || (p == 2 && q == 5)
                       || (p == 3 && q == 5);
            if (losing) correction += (int64)third - second;
        }
    }

    cout << sumMaximumOdd + sumSecondMaximumEven + correction << '\n';
    return 0;
}