题目 › usaco-1525

Lazy Sort

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

查看原题跳到结果

题面

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

Farmer John has $N$ cows ($2 \leq N \leq 5\cdot 10^6$) and is attempting to get them to sort a non-negative integer array $A$ of length $N$ by relying on their laziness. He has a lot of heavy boxes so he lines the cows up one behind another, where cow $i+1$ is behind cow $i$, and gives $a_i$ boxes to cow $i$ ($0\le a_i$).

Cows are inherently lazy so they always look to pass their work off to someone else. From cow $1$ to $N-1$ in order, each cow looks to the cow behind them. If cow $i$ has strictly more boxes than cow $i+1$, cow $i$ thinks this is "unfair" and gives one of its boxes to cow $i+1$. This process repeats until every cow is satisfied.

Farmer John will then note the number of boxes $b_i$ that each cow $i$ is holding and create an array $B$ out of these values. If $B = sorted(A)$, then Farmer John will be happy. Unfortunately, Farmer John forgot all but $Q$ values ($2 \leq Q \leq \min(N, 100)$) in $A$. Luckily, those values include the number of boxes he was going to give to the first and last cow. Each value that FJ remembers is given in the form $c_i \; v_i$ representing that $a_{c_i}=v_i$ ($1 \leq c_i \leq N$, $1\le v_i\le 10^9$). Determine the number of different ways the missing values can be filled in so that he will be happy mod $10^9+7$.

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

The first line contains two space-separated integers $N$ and $Q$ representing the number of cows and queries respectively.

The next $Q$ lines contain two space separated integers $c_i \; v_i$ representing that cow $c_i$ initially holds $v_i$ boxes. It is guaranteed that $c_1 = 1$, $c_Q = N$, and $c_i < c_{i+1}$ (the order of the cows is strictly increasing).

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

Print the number of different ways modulo $10^9+7$ that values $a_i$ can be assigned such that Farmer John will be happy after the cows perform the lazy sort. It is guaranteed that there will be at least one valid assignment.

SAMPLE INPUT:

3 2
1 3
3 2

SAMPLE OUTPUT:

2
In this example, FJ remembers the values at the ends of the array. The arrays $[3,2,2]$ and $[3,3,2]$ are the valid arrays that will make FJ happy at the end of the lazy sorting.

SAMPLE INPUT:

6 3
1 1
3 3
6 5

SAMPLE OUTPUT:

89

SCORING:

  • Inputs 3-4: $N,v_i\le 100$
  • Inputs 5-6: $N\le 100$ and $v_i \leq 10^6$
  • Inputs 7-9: $N \leq 2\cdot 10^5$ and $v_i \le 10^6$
  • Inputs 10-12: $N \leq 2\cdot 10^5$
  • Inputs 13-15: No additional constraints.

Problem credits: Suhas Nagar

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 28.1k平均输出 token(含思考) ↓
  • 25.5输出速度 tokens/s ↑
  • 1267 秒延迟 ↓

Kimi K3.1 Pro(high)Kimi high

high

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 28.1k平均输出 token(含思考) ↓
  • 25.5输出速度 tokens/s ↑
  • 1267 秒延迟 ↓

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 30.0k平均输出 token(含思考) ↓
  • 124.3输出速度 tokens/s ↑
  • 221 秒延迟 ↓

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 70.8k平均输出 token(含思考) ↓
  • 96.6输出速度 tokens/s ↑
  • 664 秒延迟 ↓

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

代码

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

static const long long MOD = 1000000007LL;
static const long long INV2 = 500000004LL;

vector<int> pow2, inv;

// S(d,h) = sum_r C(d,r) * C(h,r) * 2^(d-r).
long long calcS(int d, long long h) {
    if (h < 0) return 0;
    if (d == 0) return 1;
    if (h == 0) return pow2[d];

    if (h < d) {
        // Evaluate the sum in O(h).
        int k = (int)h;
        long long term = pow2[d];
        long long result = term;

        for (int r = 0; r < k; ++r) {
            long long factor = 1LL * (d - r) * (h - r) % MOD;
            term = term * factor % MOD;
            term = term * inv[r + 1] % MOD;
            term = term * inv[r + 1] % MOD;
            term = term * INV2 % MOD;

            result += term;
            if (result >= MOD) result -= MOD;
        }
        return result;
    }

    // Otherwise use the recurrence obtained from
    // sum_d S(d,h)x^d = (1-x)^h / (1-2x)^(h+1).
    long long hm = h % MOD;
    long long previous = 0, current = 1;

    for (int n = 0; n < d; ++n) {
        long long coefficient = (hm + 3LL * n + 2) % MOD;
        long long next = coefficient * current % MOD;

        if (n > 0) {
            next -= (2LL * n % MOD) * previous % MOD;
            if (next < 0) next += MOD;
        }

        next = next * inv[n + 1] % MOD;
        previous = current;
        current = next;
    }

    return current;
}

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

    int N, Q;
    cin >> N >> Q;

    vector<int> position(Q);
    vector<long long> value(Q);

    for (int i = 0; i < Q; ++i) {
        cin >> position[i] >> value[i];
    }

    pow2.resize(N + 1);
    inv.resize(N + 1);

    pow2[0] = 1;
    for (int i = 1; i <= N; ++i) {
        pow2[i] = 2LL * pow2[i - 1] % MOD;
    }

    inv[1] = 1;
    for (int i = 2; i <= N; ++i) {
        inv[i] = MOD - 1LL * (MOD / i) * inv[MOD % i] % MOD;
    }

    // After a fixed position with value v, the running maximum is either
    // v (low state) or v+1 (high state).
    long long dpLow = 1, dpHigh = 0;

    for (int i = 0; i + 1 < Q; ++i) {
        int d = position[i + 1] - position[i] - 1;
        long long delta = value[i + 1] - value[i];

        // Cache S(d,delta-1), S(d,delta), S(d,delta+1).
        long long heights[3] = {delta - 1, delta, delta + 1};
        long long cached[3] = {0, 0, 0};
        bool ready[3] = {false, false, false};

        auto getS = [&](int index) -> long long {
            if (index < 0 || index > 2 || heights[index] < 0) return 0;
            if (!ready[index]) {
                cached[index] = calcS(d, heights[index]);
                ready[index] = true;
            }
            return cached[index];
        };

        long long nextLow = 0, nextHigh = 0;

        auto transition = [&](long long ways, int totalIndex, int sameIndex) {
            if (ways == 0) return;

            long long total = getS(totalIndex);
            long long same = getS(sameIndex);
            long long higher = total - same;
            if (higher < 0) higher += MOD;

            nextLow = (nextLow + ways * same) % MOD;
            nextHigh = (nextHigh + ways * higher) % MOD;
        };

        // Starting maximum value[i]: h = delta+1.
        if (delta >= -1) {
            transition(dpLow, 2, 1);
        }

        // Starting maximum value[i]+1: h = delta.
        if (delta >= 0) {
            transition(dpHigh, 1, 0);
        }

        dpLow = nextLow;
        dpHigh = nextHigh;
    }

    cout << (dpLow + dpHigh) % MOD << '\n';
    return 0;
}