题解:P14977 [USACO26JAN1] Lineup Queries S

· · 题解

前言

本篇题解“主要思路”一部分像 AI,但本人语风有时候就这样,敬请包涵谅解。

同时,由于本题是模拟题,所以本文会着重对代码附件的函数进行复杂度的推导与正确性的证明(包括但不限于感性证明)。

本篇题解之前版本确实过于简略,不适合新手 / 对本题没有什么思路的读者阅读,所以导致题解被下架。笔者已经深刻反思问题,以后对题解的编写质量会反复反思,也感谢 tallnut 对本篇题解提出的批评。

如有对本篇题解的批评,欢迎指出。

主要思路

注意到两个操作互逆,所以弄出一个操作就能弄出另一个。

具体来说,利用“只有队首奶牛会被反复移动”的规律,将队列变化抽象为跳跃式数学过程,用 cls1 正向计算奶牛位置,用 cls2 反向还原位置对应奶牛,从而在 t \le 10^{18} 下用对数步回答查询(cls1 与 cls2 函数见代码)。

对操作的详细解析

第一个类型的操作

操作一:在时刻 t 刚结束时,奶牛 c 在什么位置(0\le c\le t\le 10^{18})?

做法的道理

看一下 cls1 函数:

ll cls1(ll c, ll t) {
    ll init = c * 2 - 1;
    if (t <= init) return c;
    ll pos = c;
    while (init + pos < t) {
        init += pos;
        ++init; 
        pos = init / 2;
    }
    ll diff = t - init;
    pos -= diff;
    return pos;
}

显然,当时间为 2c 时,奶牛 c 第一次出现在中点位置,所以 2c-1 为奶牛 c 最后一次不受波动的时刻。

从 2c 的时刻开始,奶牛 c 就开始向前移动,一直移动初始 pos=c 个时刻后才到位置 0,再过一个时刻才会移动到新的中点,所以新的时间 init'\leftarrow init+pos+1,而新的位置 pos'\leftarrow \lfloor\frac{init'}{2}\rfloor,符合题目要求。

就这么一直跳跃地模拟下去,直到再跳一次步就会超出所求时间时,直接减去修正量即可。

时间复杂度

看一下 cls1 函数的时间开销瓶颈:

while (init + pos < t) {
    init += pos;
    ++init; 
    pos = init / 2;
}
然后有 $init'\leftarrow init+pos+1,pos'\leftarrow\frac{init'}{2}=\frac{init+pos+1}{2}$。 记 $k$ 次循环后 $init$ 值为 $I_{k}$,则有 $$ I_{k+1}\ge I_{k}+\frac{I_{k}}{2}=\frac{3}{2}I_{k} $$ 那么 $$ I_{k}\ge(\frac{3}{2})^{k}I_{0} $$ 由代码知 `while` 循环退出条件为 $I_{k}+pos\ge t$,对不等式进行一下合理放缩,得 $I_{k}\ge\frac{t}{2}$,则 $(\frac{3}{2})^{k}I_{0}\ge\frac{t}{2}$。 不等式两边取对数,得 $$ k+\log_{3/2}{I_{0}}\ge \log_{3/2}{\frac{t}{2}}\\ k\ge log_{3/2}{(\frac{t}{2I_{0}})} $$ 所以 $k=O(\log{t})$,即时间复杂度 $O(\log{t})$。 ### 第二个类型的操作 > 操作二:在时刻 $t$ 刚结束时,位置 $x$ 上是哪头奶牛($0\le x\le t\le 10^{18}$)? 这部分虽然是类型一的逆操作,但是更难些,这里详细讲一讲做法的道理。 #### 做法的道理 还是先看一下 `cls2` 函数: ```cpp line-numbers ll cls2(ll x, ll t) { ll pos = x; ll s = t; while (true) { if (pos == s) return s; if (s == 0) return 0; if (s < 2 * pos) return pos; ll d = (s - 2 * pos) / 3; if (d > 0) { pos += d; s -= d; continue; } ll k = s / 2; if (pos == k) pos = 0; else pos += 1; --s; } } ``` 用几句人话把代码中的关键变量说清楚。 下面令答案牛编号为 $ans$。 - $pos$:初始化为 $x$,表示 $ans$ 在时间回溯的模拟过程中的**实时位置**。 - $s$:表示现在模拟到的时刻。 - $d$:修正量。 对应着 `cls1`,`cls2` 也要有大撤步的操作才能节省时间,这部分的代码如下。 ```cpp line-numbers ll d = (s - 2 * pos) / 3; if (d > 0) { pos += d; s -= d; continue; } ``` 为什么修正量是这么诡异的公式?我们来详细说明一下。 正向跳 `cls1` 中,有关系 $$ pos'\approx\frac{init'}{2}\\ $$ 所以如果还能继续往前跳,则 $$ init'\ge2\cdot pos' $$ 让我们想一下逆向跳 $d$ 步而不至于跳回位置 $0$ 时,各变量的回溯规律: - $pos$ 要回退(向右)着跳 $d$ 步,即 $pos'\leftarrow pos+d$。 - $s$ 作为时刻当然要回退 $d$ 刻,即 $s'\leftarrow s-d$。 不至于跳回位置 $0$,含义是什么?其实就是还能继续往前跳。 那么根据 `cls1` 与 `cls2` 的变量间关系套用上面不等式,设出并解得 $$ \begin{aligned} s-d\ge2(pos+d)&\Rightarrow s-2\cdot pos\ge3d\\ &\Rightarrow d\le\frac{s-2\cdot pos}{3} \end{aligned} $$ 那么 $d$ 取 $\max$ 时,$d=\frac{s-2\cdot pos}{3}$。 所以上面代码的大跨步部分合理。 小碎步部分较简单,直接对应 `cls1` 即可。 当 $pos'=s'$ 时,再结合题意,可知此时 $pos'$ 便回溯到了 $ans$ 号牛最初的来处。 所以此时应该终止回溯,$ans=pos'$。 #### 时间复杂度 可见,`cls2` 的过程完全就是 `cls1` 的反向,只不过其过程中的数学计算方面更加困难,但数学计算依旧 $O(1)$,所以 `cls2` 函数的时间复杂度依旧为 $O(\log{t})$。 ## Code [Record.](https://www.luogu.com.cn/record/258044622) ```cpp line-numbers #include <bits/stdc++.h> using namespace std; using ll = long long; ll T; ll cls1(ll c, ll t) { ll init = c * 2 - 1; if (t <= init) return c; ll pos = c; while (init + pos < t) { init += pos; ++init; pos = init / 2; } ll diff = t - init; pos -= diff; return pos; } ll cls2(ll x, ll t) { ll pos = x; ll s = t; while (true) { if (pos == s) return s; if (s == 0) return 0; if (s < 2 * pos) return pos; ll d = (s - 2 * pos) / 3; if (d > 0) { pos += d; s -= d; continue; } ll k = s / 2; if (pos == k) pos = 0; else pos += 1; --s; } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> T; while (T--) { ll op; cin >> op; if (op == 1) { ll c, t; cin >> c >> t; cout << cls1(c, t) << "\n"; } else { ll x, t; cin >> x >> t; cout << cls2(x, t) << "\n"; } } } ```