P10038 (Official)

· · 题解

Part 0

Update on 2026/8/16:完全重写了 std 和题解。

在密码锁中,有一条长度为 n(不能更改,n 的具体取值见密码锁铭牌)的链,链上共有 n 个结点。每个结点上可以存放至多一个原子。初始时,1,2,\ldots,n 号原子以某个顺序(可以由用户自行调整)被存放在其中,每个结点存放一个原子。

定义 i 号原子的电荷量为 i!=1 \times 2\times 3 \times \ldots \times i。

现有一个计时器 b(单位为秒),其初值为 0。

密码锁被加热后,以下事件依次循环发生,直至达成终止条件:

  1. 位于链两端的原子被移除(这不会使链变短),不再对后续事件产生影响;
  2. 判定终止条件:
    • 若此时链中剩下不多于 \bm 1 个原子(也可以是 \bm 0 个),则达成终止条件,密码锁瘫痪(此时计时器 \bm b 的值不会增加);
    • 否则,将计时器 b 的值增加 \color{red}a_c,其中 c 是链中剩下的原子个数。
  3. 给每个原子标定运动方向(标定的运动方向是临时的,只生效一次,在下一次标定前会被重置):
    • 计算它左边所有原子的电荷量之和,设计算结果为 x;
    • 计算它右边所有原子的电荷量之和,设计算结果为 y;
    • 如果 x<y,则标定方向为「向左」;
    • 如果 x>y,则标定方向为「向右」;
    • 可以证明,x \ne y。
  4. 所有原子按照所标定的运动方向,移动一条边的距离,来到相邻的结点。

(本题为以上题目在 a_i = 1 时的弱化版,以下是原版解法。)

Part 1

下面的「最大编号原子」代指链中编号最大的原子。

首先不难发现:n!>(n-1)!+(n-2)!+\ldots+1!。证明不给了。

于是原子的运动方式简化为:

由于初始状态(n 个原子)和最终状态(0/1 个原子)中的原子都形成一个区间,我们考虑区间 DP。先看一看,当原子形成一个区间时,是什么情况(以下是一个例子):

(n = 14)
Start:
(--) -- -- -- 08 04 02 09 14 06 11 05 -- (--)
Steps:
[Stage 1]
(--) -- -- -- 08 04 02 09 14 06 11 05 -- (--)
(--) -- -- 08 04 02 09 14 -- -- 06 11 05 (--)
[Stage 2]
(--) -- 08 04 02 09 14 -- -- -- -- 06 11 (05)
(--) 08 04 02 09 14 -- -- -- -- -- -- 06 (11)
(08) 04 02 09 -- -- 14 -- -- -- -- -- -- (06)
[Stage 3]
(04) 02 09 -- -- -- -- 14 -- -- -- -- -- (--)
(02) 09 -- -- -- -- -- -- 14 -- -- -- -- (--)
(09) -- -- -- -- -- -- -- -- 14 -- -- -- (--)
Stop:
(--) -- -- -- -- -- -- -- -- 14 -- -- -- (--)

不难发现,这个区间被分裂成三段,其中最大编号原子是分界线:

这个运动过程也先后分为三个阶段:

此外,还有以下特点:

于是我们可以区间 DP 了。定义 g_{i, j}\ (2 \le i \le j \le n - 1) 为:当剩下的原子形成区间 [i,j] 且不能确定任何原子的编号及其相对大小关系时,所经过的最长可能时间,其中 g_{2, n - 1} = 0。

在状态转移时,枚举最大编号原子的位置 k 和次大编号原子的位置 l 即可,并认为和次大编号原子位于最大编号原子同侧的其他原子都拥有足够小的编号。

但这(看上去)并不一定合法,因为这一规定破坏了后效性,但我们可以:

最优解的构造已经在上面的讨论中给出。

最终的时间复杂度为 O(n^4),空间复杂度为 O(n^2)。

Part 2

#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 12;
int w[N][N][6], a[N][3], q[N];
void consume(int n, int l, int r, int f, int s, int& x, int& y, int& z, int& u);
int stopwatch(int c, int s, int e)
{
    if (s) return a[c][0] * s + stopwatch(c, 0, e - s);
    return a[c - 1][1] - a[c - e - 1][1];
}
int stopwatch(int c, int l, int r, int x, int y)
{
    if (r > y) return stopwatch(c, x, y, l, r);
    if (r <= x) return stopwatch(c, l, r);
    if (l && x)
    {
        int z = min(l, x);
        return a[c][0] * z + stopwatch(c, l - z, r - z, x - z, y - z);
    }
    if (!l && !x) return a[c - 2][2] - a[c - r * 2 - 2][2];
    if (!l) return stopwatch(c, 0, x) + stopwatch(c - x, 0, r - x, 0, y - x);
    return stopwatch(c, 0, l) + stopwatch(c - l, 0, r - l, 0, y - l);
}
void finals(int n, int l, int r, int c, int d, int f, int& x, int& y, int& z, int& u)
{
    l = max(l, 2);
    d = min(d, n - 1);
    if (r <= 1 && c >= n)
    {
        x = f;
        y = f;
        u = f;
        return;
    }
    if (r <= 1)
    {
        finals(n, n + 1 - d, n + 1 - c, n, n, n + 1 - f, x, y, z, u);
        swap(x, y);
        if (x) x = n + 1 - x;
        if (y) y = n + 1 - y;
        u = n + 1 - u;
        return;
    }
    int t = (f - r - 1) / 2;
    z -= a[r - l + 2][0] * t;
    consume(n, l + t, f - t, f - t, l + t, x, y, z, u);
}
void consume(int n, int l, int r, int f, int s, int& x, int& y, int& z, int& u)
{
    if (l == r)
    {
        x = l;
        y = r;
        u = r;
        return;
    }
    if (s < f)
    {
        consume(n, n + 1 - r, n + 1 - l, n + 1 - f, n + 1 - s, x, y, z, u);
        swap(x, y);
        if (x) x = n + 1 - x;
        if (y) y = n + 1 - y;
        u = n + 1 - u;
        return;
    }
    if (f == l)
    {
        if (r + l - 1 <= n - 1)
        {
            x = l + l;
            y = r + l - 1;
            z += stopwatch(r - l + 1, l - 1, l - 1);
            u = 0;
            return;
        }
        if (l + l >= n)
        {
            x = l + l + 1 - n;
            y = l + l + 1 - n;
            z += stopwatch(r - l + 1, n - r, n - l - 1);
            u = l + l + 1 - n;
            if (l + l == n)
            {
                x = 0;
                y = 0;
                u = 0;
            }
            return;
        }
        x = l + l;
        y = n - 1;
        z += stopwatch(r - l + 1, n - r, l - 1);
        u = 0;
        return;
    }
    z += stopwatch(r - l + 1, l - 1, f - 2, n - r, n - f - 1);
    int t = min(f - 2, n - f - 1), v = f - t + 2 * max(t - n + s, 0);
    finals(n, l - t, f - 1 - t, f + 1 + t, r + t, v, x, y, z, u);
}
void determine(int c, int d, int e, int g, int x, int y, int& l, int& r, deque <int> & f, deque <int> & b)
{
    for (int i = c; i <= d; i++)
        q[i] = 0;
    q[e] = r;
    r--;
    int u = -1;
    if (e != c && e != d && (g < x || g > y || x == y))
    {
        q[g] = r;
        r--;
        u = 1;
    }
    for (int i = c; i <= d; i++)
    {
        if (i >= x && i <= y && x != y || q[i]) continue;
        if ((g - e) * (i - e) * u > 0)
        {
            q[i] = l;
            l++;
            continue;
        }
        q[i] = r;
        r--;
    }
    if (x == y)
    {
        for (int i = c; i <= d; i++)
            f.push_back(q[i]);
        return;
    }
    for (int i = c; i <= x - 1; i++)
        f.push_back(q[i]);
    for (int i = d; i >= y + 1; i--)
        b.push_back(q[i]);
}
int main()
{
    int n;
    cin >> n;
    if (n <= 6)
    {
        for (int i = 1; i <= n - 1; i++)
            cout << i << ' ';
        cout << n << endl;
        return 0;
    }
    for (int i = 1; i <= n; i++)
    {
        a[i][0] = 1;
        a[i][1] = i;
        a[i][2] = (i + 1) / 2;
    }
    memset(w, -1, sizeof w);
    w[2][n - 1][4] = 0;
    for (int s = n - 2; s >= 2; s--)
        for (int i = 2, j = s + 1; j <= n - 1; i++, j++)
        {
            if (w[i][j][4] == -1) continue;
            for (int k = i; k <= j; k++)
                for (int l = i; l <= j; l++)
                {
                    if (k == l) continue;
                    int x = 0, y = 0, z = w[i][j][4], u = 0;
                    consume(n, i, j, k, l, x, y, z, u);
                    if (z > w[x][y][4])
                    {
                        w[x][y][0] = i;
                        w[x][y][1] = j;
                        w[x][y][2] = k;
                        w[x][y][3] = l;
                        w[x][y][4] = z;
                        w[x][y][5] = u;
                    }
                }
        }
    deque <int> f, b, x, y;
    int l = 1, r = n - 2, u = 0, v = 0;
    for (int i = 2; i <= n - 1; i++)
        if (w[i][i][4] > w[u][v][4])
        {
            u = i;
            v = i;
        }
    while (u != 2 || v != n - 1)
    {
        x.push_back(u);
        y.push_back(v);
        int c = w[u][v][0], d = w[u][v][1];
        u = c;
        v = d;
    }
    while (!x.empty())
    {
        int c = x.back(), d = y.back(), e = w[c][d][2], g = w[c][d][3], h = w[c][d][5];
        x.pop_back();
        y.pop_back();
        if (!h) determine(u, v, e, g, e + 1, e + d - c + 1, l, r, f, b);
        if (h == n + 1) determine(u, v, e, g, e - d - 1 + c, e - 1, l, r, f, b);
        if (h && h != n + 1) determine(u, v, e, g, e, e, l, r, f, b);
        u = c;
        v = d;
        if (u == v) break;
    }
    if (f.size() + b.size() < n - 2) b.push_back(r);
    cout << n - 1 << ' ';
    while (!f.empty())
    {
        cout << f.front() << ' ';
        f.pop_front();
    }
    while (!b.empty())
    {
        cout << b.back() << ' ';
        b.pop_back();
    }
    cout << n << endl;
    return 0;
}

Part 3

你可以在这里找到原题解的存档,它以 Base64 形式呈现。