题目 › usaco-1542

Lineup Queries

USACO 2026 First · 银组 · 2026-01 · L2 中等 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

There is a line of cows, initially (i.e. at time $t = 0$) containing only cow $0$ at position $0$ (here, a cow is at position $k$ if there are $k$ cows in front of it). At time $t$ for $t=1,2,3,\dots$, the cow at position 0 moves to position $\lfloor t/2\rfloor$, every cow in positions $1\dots \lfloor t/2\rfloor$ moves forward one position, and cow $t$ joins the line at the end of the line (position $t$).

Answer $Q$ ($1\le Q\le 10^5$) independent queries, each of one of the following types:

  1. At what position is cow $c$ immediately after time $t$ ($0\le c\le t\le 10^{18}$)?
  2. Which cow is at position $x$ immediately after time $t$ ($0\le x\le t\le 10^{18}$)?

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

The first line contains $Q$, the number of queries.

The next $Q$ lines each contain three integers specifying a query either of the form "1 c t" or "2 x t."

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

Output the answer to each query on a separate line.

SAMPLE INPUT:

2
1 4 9
2 2 9

SAMPLE OUTPUT:

2
4

Lineups immediately after various times:

t = 0 | 0
t = 1 | 0 1
t = 2 | 1 0 2
t = 3 | 0 1 2 3
t = 4 | 1 2 0 3 4
t = 5 | 2 0 1 3 4 5
t = 6 | 0 1 3 2 4 5 6
t = 7 | 1 3 2 0 4 5 6 7
t = 8 | 3 2 0 4 1 5 6 7 8
t = 9 | 2 0 4 1 3 5 6 7 8 9

Immediately after $t=9$, the location of cow $4$ is $2$, and the cow located at position $2$ is $4$.

SAMPLE INPUT:

22
1 0 9
1 1 9
1 2 9
1 3 9
1 4 9
1 5 9
1 6 9
1 7 9
1 8 9
1 9 9
2 0 9
2 1 9
2 2 9
2 3 9
2 4 9
2 5 9
2 6 9
2 7 9
2 8 9
2 9 9
1 0 1000000000000000000
2 0 1000000000000000000

SAMPLE OUTPUT:

1
3
0
4
2
5
6
7
8
9
2
0
4
1
3
5
6
7
8
9
483992463350322770
148148148148148148

SCORING:

  • Input 3: $Q\le 1000, t\le 100$
  • Input 4: $t\le 5000$
  • Inputs 5-8: All queries are of type 1.
  • Inputs 9-12: All queries are of type 2.

Problem credits: Agastya Goel

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 11.1k平均输出 token(含思考) ↓
  • 28.3输出速度 tokens/s ↑
  • 394 秒延迟 ↓
  • 0.50×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 9.09Pylint 得分 ↑
  • 14圈复杂度 ↓
  • 54.9可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 21.5k平均输出 token(含思考) ↓
  • 226.3输出速度 tokens/s ↑
  • 96 秒延迟 ↓
  • 0.45×CPU 时间 / 官方解 ↓
  • 不可测时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 9.05Pylint 得分 ↑
  • 12圈复杂度 ↓
  • 53.1可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 151.5k平均输出 token(含思考) ↓
  • 467.9输出速度 tokens/s ↑
  • 324 秒延迟 ↓
  • 0.30×CPU 时间 / 官方解 ↓
  • 更优时间复杂度(相对官方解)
  • 5Pylint 问题 ↓
  • 8.96Pylint 得分 ↑
  • 15圈复杂度 ↓
  • 58.1可维护性指数 ↑

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

代码

Kimi K3.1 Pro · 采样 1 · 65 行
import sys


def cow_position(c, t):
    if c == 0:
        front = 0
    else:
        first_front = 3 * c - 1
        if t < first_front:
            if t < 2 * c:
                return c
            return first_front - t
        front = first_front

    while front < t:
        next_front = (3 * (front + 1)) // 2
        if next_front > t:
            return next_front - t
        front = next_front

    return 0


def cow_at_position(x, t):
    if x > t // 2:
        return x

    # This cow is (or will be) at the front at time front_time.
    front_time = t + x

    while front_time:
        r = front_time % 3
        if r == 2:
            # First time at the front for cow c: time = 3*c - 1.
            return (front_time + 1) // 3
        if r == 0:
            front_time = 2 * (front_time // 3) - 1
        else:
            front_time = 2 * ((front_time - 1) // 3)

    return 0


def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    q = data[0]
    answers = []
    idx = 1

    for _ in range(q):
        query_type = data[idx]
        value = data[idx + 1]
        t = data[idx + 2]
        idx += 3

        if query_type == 1:
            answers.append(str(cow_position(value, t)))
        else:
            answers.append(str(cow_at_position(value, t)))

    sys.stdout.write("\n".join(answers))


if __name__ == "__main__":
    main()