跳到正文

题解

题解:P14977 [USACO26JAN1] Lineup Queries S

Wyh_dailyAC约 9 分钟
原文
文章信息与版本

文章信息

收录
2026年1月19日
更新
2026年1月19日
篇幅
约 9 分钟
题目
P14977

版本

  1. v2当前
  2. v1

前言

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

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

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

主要思路

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

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

对操作的详细解析

第一个类型的操作

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

做法的道理

看一下 cls1 函数:

C++
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;
}

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

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

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

时间复杂度

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

C++
while (init + pos < t) {
    init += pos;
    ++init;
    pos = init / 2;
}

initinit 的初始值是 2c−12c-1,pospos 初始值为 cc。

然后有 init′←init+pos+1,pos′←init′2=init+pos+12init'\leftarrow init+pos+1,pos'\leftarrow\frac{init'}{2}=\frac{init+pos+1}{2}。

记 kk 次循环后 initinit 值为 IkI_{k},则有

Ik+1≥Ik+Ik2=32IkI_{k+1}\ge I_{k}+\frac{I_{k}}{2}=\frac{3}{2}I_{k}

那么

Ik≥(32)kI0I_{k}\ge(\frac{3}{2})^{k}I_{0}

由代码知 while 循环退出条件为 Ik+pos≥tI_{k}+pos\ge t,对不等式进行一下合理放缩,得 Ik≥t2I_{k}\ge\frac{t}{2},则 (32)kI0≥t2(\frac{3}{2})^{k}I_{0}\ge\frac{t}{2}。

不等式两边取对数,得

k+log⁡3/2I0≥log⁡3/2t2k≥log3/2(t2I0)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)k=O(\log{t}),即时间复杂度 O(log⁡t)O(\log{t})。

第二个类型的操作

操作二:在时刻 tt 刚结束时,位置 xx 上是哪头奶牛(0≤x≤t≤10180\le x\le t\le 10^{18})?

这部分虽然是类型一的逆操作,但是更难些,这里详细讲一讲做法的道理。

做法的道理

还是先看一下 cls2 函数:

C++
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;
    }
}

用几句人话把代码中的关键变量说清楚。

下面令答案牛编号为 ansans。

  • pospos:初始化为 xx,表示 ansans 在时间回溯的模拟过程中的实时位置。
  • ss:表示现在模拟到的时刻。
  • dd:修正量。

对应着 cls1,cls2 也要有大撤步的操作才能节省时间,这部分的代码如下。

C++
ll d = (s - 2 * pos) / 3;
if (d > 0) {
    pos += d;
    s -= d;
    continue;
}

为什么修正量是这么诡异的公式?我们来详细说明一下。

正向跳 cls1 中,有关系

pos′≈init′2pos'\approx\frac{init'}{2}\\

所以如果还能继续往前跳,则

init′≥2⋅pos′init'\ge2\cdot pos'

让我们想一下逆向跳 dd 步而不至于跳回位置 00 时,各变量的回溯规律:

  • pospos 要回退(向右)着跳 dd 步,即 pos′←pos+dpos'\leftarrow pos+d。
  • ss 作为时刻当然要回退 dd 刻,即 s′←s−ds'\leftarrow s-d。

不至于跳回位置 00,含义是什么?其实就是还能继续往前跳。

那么根据 cls1 与 cls2 的变量间关系套用上面不等式,设出并解得

s−d≥2(pos+d)⇒s−2⋅pos≥3d⇒d≤s−2⋅pos3\begin{aligned} s-d\ge2(pos+d)&\Rightarrow s-2\cdot pos\ge3d\\ &\Rightarrow d\le\frac{s-2\cdot pos}{3} \end{aligned}

那么 dd 取 max⁡\max 时,d=s−2⋅pos3d=\frac{s-2\cdot pos}{3}。

所以上面代码的大跨步部分合理。

小碎步部分较简单,直接对应 cls1 即可。

当 pos′=s′pos'=s' 时,再结合题意,可知此时 pos′pos' 便回溯到了 ansans 号牛最初的来处。

所以此时应该终止回溯,ans=pos′ans=pos'。

时间复杂度

可见,cls2 的过程完全就是 cls1 的反向,只不过其过程中的数学计算方面更加困难,但数学计算依旧 O(1)O(1),所以 cls2 函数的时间复杂度依旧为 O(log⁡t)O(\log{t})。

Code

Record.

C++
#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";
        }
    }
}
原文