题目 › usaco-1454

Maximize Minimum Difference

USACO 2024 December · 铂金组 · 2024-12 · L4 顶尖 · C++17 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

**Note: The time limit for this problem is 4s, twice the default.**

Moo! You are given an integer $N$ ($2\le N\le 2000$). Consider all permutations $p=[p_0,p_1,\dots, p_{N-1}]$ of $[0,1,2\dots, N-1]$. Let $f(p)=\min_{i=0}^{N-2}|p_i-p_{i+1}|$ denote the minimum absolute difference between any two consecutive elements of $p$. and let $S$ denote the set of all $p$ that achieve the maximum possible value of $f(p)$.

You are additionally given $K$ ($0\le K\le N$) constraints of the form $p_i=j$ ($0\le i,j<N$). Count the number of permutations in $S$ satisfying all constraints, modulo $10^9+7$.

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

The first line contains $T$ ($1\le TN\le 2\cdot 10^4$) and $N$, meaning that you will need to solve $T$ independent test cases, each specified by a different set of constraints.

Each test case starts with $K$, followed by $K$ lines each containing $i$ and $j$. It is guaranteed that

  • The same $i$ does not appear more than once within the same test case.
  • The same $j$ does not appear more than once within the same test case.

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

For each test case, the answer modulo $10^9+7$ on a separate line.

SAMPLE INPUT:

3 4
0
1
1 1
2
0 2
2 3

SAMPLE OUTPUT:

2
0
1

The maximum possible value of $f(p)$ is $2$, and $S=\{[2,0,3,1], [1,3,0,2]\}$.

SAMPLE INPUT:

9 11
2
0 5
6 9
3
0 5
6 9
1 0
4
0 5
6 9
1 0
4 7
5
0 5
6 9
1 0
4 7
2 6
6
0 5
6 9
1 0
4 7
2 6
9 3
7
0 5
6 9
1 0
4 7
2 6
9 3
5 2
8
0 5
6 9
1 0
4 7
2 6
9 3
5 2
7 4
9
0 5
6 9
1 0
4 7
2 6
9 3
5 2
7 4
3 1
10
0 5
6 9
1 0
4 7
2 6
9 3
5 2
7 4
3 1
8 10

SAMPLE OUTPUT:

6
6
1
1
1
1
1
1
1

$p=[5, 0, 6, 1, 7, 2, 9, 4, 10, 3, 8]$ should be counted for all test cases.

SAMPLE INPUT:

10 11
0
1
3 8
2
3 8
5 7
3
3 8
5 7
4 2
4
3 8
5 7
4 2
10 6
5
3 8
5 7
4 2
10 6
8 10
6
3 8
5 7
4 2
10 6
8 10
1 9
7
3 8
5 7
4 2
10 6
8 10
1 9
7 5
8
3 8
5 7
4 2
10 6
8 10
1 9
7 5
2 3
9
3 8
5 7
4 2
10 6
8 10
1 9
7 5
2 3
6 0

SAMPLE OUTPUT:

160
20
8
7
2
1
1
1
1
1

$p=[4, 9, 3, 8, 2, 7, 0, 5, 10, 1, 6]$ should be counted for all test cases.

SAMPLE INPUT:

5 987
3
654 321
543 210
432 106
2
654 321
543 210
1
654 321
1
0 493
0

SAMPLE OUTPUT:

0
538184948
693625420
932738155
251798971

Make sure to output the answer modulo $10^9+7$.

SCORING:

  • Input 5: $N=15$
  • Input 6: $N=2000$
  • Inputs 7-9: For all test cases, the constraint $p_0=\lfloor N/2\rfloor$ appears.
  • Inputs 10-13: For all test cases, there exists some constraint $p_i = j$ with $j$ equal to $\lfloor N/2\rfloor$.
  • Inputs 14-20: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 52.5k平均输出 token(含思考) ↓
  • 24.3输出速度 tokens/s ↑
  • 2288 秒延迟 ↓
  • 0.97×CPU 时间 / 官方解 ↓
  • 不确定 2、同阶 1时间复杂度(相对官方解)

Kimi K3.1 Pro(high)Kimi high

high

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 52.5k平均输出 token(含思考) ↓
  • 24.3输出速度 tokens/s ↑
  • 2288 秒延迟 ↓
  • 0.97×CPU 时间 / 官方解 ↓
  • 不确定 2、同阶 1时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

0/3

解出的采样 ↑

  • 0.0采样 1 · 答案错误
  • 10.0采样 2 · 答案错误
  • 15.0采样 3 · 答案错误
  • 116.7k平均输出 token(含思考) ↓
  • 189.1输出速度 tokens/s ↑
  • 635 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • —时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

0/3

解出的采样 ↑

  • 10.0采样 1 · 答案错误
  • 0.0采样 2 · 编译错误
  • 0.0采样 3 · 编译错误
  • 148.0k平均输出 token(含思考) ↓
  • 104.3输出速度 tokens/s ↑
  • 1059 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • —时间复杂度(相对官方解)

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

代码

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

static const long long MOD = 1000000007LL;

int N, M;
vector<long long> factv, invfactv, pow2v;
vector<int> wantPos, wantVal;

long long modpow(long long a, long long e) {
    long long r = 1;
    while (e) {
        if (e & 1) r = r * a % MOD;
        a = a * a % MOD;
        e >>= 1;
    }
    return r;
}

long long comb(int n, int k) {
    if (k < 0 || k > n || n < 0) return 0;
    return factv[n] * invfactv[k] % MOD * invfactv[n - k] % MOD;
}

bool fixedOK(int v, int pos) {
    if (pos < 0 || pos >= N) return false;
    if (wantVal[v] != -1 && wantVal[v] != pos) return false;
    if (wantPos[pos] != -1 && wantPos[pos] != v) return false;
    return true;
}

// type 0: [q, M+q], type 1: [M+q, q].
bool tokenOK(int q, int type, int start) {
    if (type == 0) {
        return fixedOK(q, start) && fixedOK(M + q, start + 1);
    } else {
        return fixedOK(M + q, start) && fixedOK(q, start + 1);
    }
}

struct Event {
    int t, q;
};

struct Layout {
    int kind;       // 0: generic two-sided layout, 1: form with two F-blocks sides
    int leftBase;
    int rightEnd;
    int rightBase;
    int selectedType;
    int unselectedType;
};

int placementStart(const Layout& lay, bool selected, int c, int t) {
    if (lay.kind == 0) {
        if (selected) return lay.leftBase + 2 * c;
        return lay.rightEnd - 2 * (t - c + 1);
    } else {
        if (selected) return 2 * c;
        return lay.rightBase + 2 * (t - c);
    }
}

bool getCandidate(const Layout& lay, bool selected, int q, int t, int& cOut) {
    int type = selected ? lay.selectedType : lay.unselectedType;
    int start = -1;

    if (wantVal[q] != -1) {
        int offset = (type == 0 ? 0 : 1);
        start = wantVal[q] - offset;
    } else if (wantVal[M + q] != -1) {
        int offset = (type == 0 ? 1 : 0);
        start = wantVal[M + q] - offset;
    } else {
        return false;
    }

    int c = -1;
    if (lay.kind == 0) {
        if (selected) {
            int diff = start - lay.leftBase;
            if (diff < 0 || (diff & 1)) return false;
            c = diff / 2;
        } else {
            int diff = lay.rightEnd - start;
            if (diff <= 0 || (diff & 1)) return false;
            c = t + 1 - diff / 2;
        }
    } else {
        if (selected) {
            if (start < 0 || (start & 1)) return false;
            c = start / 2;
        } else {
            int diff = start - lay.rightBase;
            if (diff < 0 || (diff & 1)) return false;
            c = t - diff / 2;
        }
    }

    if (c < 0 || c > t) return false;
    int actualStart = placementStart(lay, selected, c, t);
    if (actualStart != start) return false;
    if (!tokenOK(q, type, start)) return false;

    cOut = c;
    return true;
}

long long countAssignments(int h, const vector<Event>& events,
                           int totalRequired, const Layout& lay) {
    if (totalRequired > h) return 0;

    vector<pair<int, long long>> states;
    states.push_back({0, 1});
    int processed = 0;

    for (const Event& ev : events) {
        int gap = ev.t - processed;
        vector<pair<int, long long>> nextStates;

        for (int sel = 0; sel <= 1; ++sel) {
            int c;
            if (!getCandidate(lay, sel, ev.q, ev.t, c)) continue;

            long long ways = 0;
            for (auto [oldC, oldWays] : states) {
                ways = (ways + oldWays * comb(gap, c - oldC)) % MOD;
            }
            if (!ways) continue;

            int newC = c + sel;
            bool merged = false;
            for (auto& entry : nextStates) {
                if (entry.first == newC) {
                    entry.second = (entry.second + ways) % MOD;
                    merged = true;
                    break;
                }
            }
            if (!merged) nextStates.push_back({newC, ways});
        }

        if (nextStates.empty()) return 0;
        states.swap(nextStates);
        processed = ev.t + 1;
    }

    int remaining = h - processed;
    long long answer = 0;

    if (totalRequired == -1) {
        for (auto [c, ways] : states) {
            answer = (answer + ways * pow2v[remaining]) % MOD;
        }
    } else {
        for (auto [c, ways] : states) {
            answer = (answer + ways * comb(remaining, totalRequired - c)) % MOD;
        }
    }
    return answer;
}

vector<Event> makeEvents(int lo, int hi, int mode) {
    vector<Event> events;
    if (lo > hi) return events;

    if (mode == 0) {
        for (int q = hi; q >= lo; --q) {
            if (wantVal[q] != -1 || wantVal[M + q] != -1) {
                events.push_back({hi - q, q});
            }
        }
    } else {
        for (int q = lo; q <= hi; ++q) {
            if (wantVal[q] != -1 || wantVal[M + q] != -1) {
                events.push_back({q - lo, q});
            }
        }
    }
    return events;
}

// F/R two-sided layout with one variable-position middle token.
long long genericCount(int lo, int hi, int leftBase, int rightEnd,
                       int middleValue, int mode) {
    int h = max(0, hi - lo + 1);
    vector<Event> events = makeEvents(lo, hi, mode);

    int totalRequired = -1;
    if (wantVal[middleValue] != -1) {
        int pos = wantVal[middleValue];
        int diff = pos - leftBase;
        if (diff < 0 || (diff & 1)) return 0;
        totalRequired = diff / 2;
        if (totalRequired < 0 || totalRequired > h) return 0;
        if (!fixedOK(middleValue, pos)) return 0;
    }

    Layout lay;
    lay.kind = 0;
    lay.leftBase = leftBase;
    lay.rightEnd = rightEnd;
    lay.rightBase = 0;

    if (mode == 0) {
        lay.selectedType = 0;
        lay.unselectedType = 1;
    } else {
        lay.selectedType = 1;
        lay.unselectedType = 0;
    }

    return countAssignments(h, events, totalRequired, lay);
}

long long countCanonicalOdd() {
    int h = M - 1;
    long long answer = 0;

    // [M,0], R(Q) increasing, N-1, F(S) decreasing.
    if (fixedOK(M, 0) && fixedOK(0, 1)) {
        answer += genericCount(1, M - 1, 2, N, N - 1, 1);
    }

    // F(P) decreasing, [0,M,N-1], F(S) decreasing.
    vector<Event> middleEvents = makeEvents(1, M - 1, 0);
    for (int p = 0; p <= h; ++p) {
        int a = 2 * p;
        if (!fixedOK(0, a) || !fixedOK(M, a + 1) ||
            !fixedOK(N - 1, a + 2)) {
            continue;
        }

        Layout lay;
        lay.kind = 1;
        lay.leftBase = 0;
        lay.rightEnd = 0;
        lay.rightBase = 2 * p + 3;
        lay.selectedType = 0;
        lay.unselectedType = 0;

        answer += countAssignments(h, middleEvents, p, lay);
        if (answer >= MOD) answer -= MOD;
    }

    // F(P) decreasing, 0, R(Q) increasing, [N-1,M].
    if (fixedOK(N - 1, N - 2) && fixedOK(M, N - 1)) {
        answer += genericCount(1, M - 1, 0, N - 2, 0, 0);
    }
    answer %= MOD;

    // Defective forms, containing [N-1,M,0].
    for (int d = 1; d <= M - 1; ++d) {
        int highCount = M - 1 - d;
        int posB = 2 * highCount + 1;

        if (!fixedOK(N - 1, posB) ||
            !fixedOK(M, posB + 1) ||
            !fixedOK(0, posB + 2)) {
            continue;
        }

        long long highWays = genericCount(
            d + 1, M - 1, 0, posB, d, 0
        );
        if (!highWays) continue;

        long long lowWays = genericCount(
            1, d - 1, posB + 3, N, M + d, 1
        );

        answer = (answer + highWays * lowWays) % MOD;
    }

    return answer;
}

long long countCanonicalEven() {
    vector<int> seq;
    seq.reserve(N);
    for (int k = 0; k < M; ++k) {
        seq.push_back(M - 1 - k);
        seq.push_back(2 * M - 1 - k);
    }

    for (int i = 0; i < N; ++i) {
        if (wantPos[i] != -1 && wantPos[i] != seq[i]) return 0;
    }
    return 1;
}

long long solveCanonical(const vector<pair<int, int>>& constraints, bool reversed) {
    wantPos.assign(N, -1);
    wantVal.assign(N, -1);

    for (auto [i, j] : constraints) {
        int pos = reversed ? N - 1 - i : i;
        wantPos[pos] = j;
        wantVal[j] = pos;
    }

    if (N % 2 == 0) return countCanonicalEven();
    return countCanonicalOdd();
}

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

    int T;
    cin >> T >> N;
    M = N / 2;

    factv.assign(N + 1, 1);
    for (int i = 1; i <= N; ++i) {
        factv[i] = factv[i - 1] * i % MOD;
    }
    invfactv.assign(N + 1, 1);
    invfactv[N] = modpow(factv[N], MOD - 2);
    for (int i = N; i >= 1; --i) {
        invfactv[i - 1] = invfactv[i] * i % MOD;
    }

    pow2v.assign(N + 1, 1);
    for (int i = 1; i <= N; ++i) {
        pow2v[i] = pow2v[i - 1] * 2 % MOD;
    }

    while (T--) {
        int K;
        cin >> K;
        vector<pair<int, int>> constraints(K);
        for (int i = 0; i < K; ++i) {
            cin >> constraints[i].first >> constraints[i].second;
        }

        long long answer = solveCanonical(constraints, false);
        answer += solveCanonical(constraints, true);
        answer %= MOD;
        cout << answer << '\n';
    }

    return 0;
}