题目 › usaco-1493

Printing Sequences

USACO 2025 February · 铜组 · 2025-02 · L1 基础 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie is learning to code using a simple programming language. She first defines a valid program, then executes it to produce some output sequence.

Defining:

  • A program is a nonempty sequence of statements.
  • A statement is either of the form "PRINT $c$" where $c$ is an integer, or "REP $o$", followed by a program, followed by "END," where $o$ is an integer that is at least 1.
Executing:
  • Executing a program executes its statements in sequence.
  • Executing the statement "PRINT $c$" appends $c$ to the output sequence.
  • Executing a statement starting with "REP $o$" executes the inner program a total of $o$ times in sequence.

An example of a program that Bessie knows how to write is as follows.

REP 3
    PRINT 1
    REP 2
        PRINT 2
    END
END

The program outputs the sequence $[1,2,2,1,2,2,1,2,2]$.

Bessie wants to output a sequence of $N$ ($1 \le N \le 100$) positive integers. Elsie challenges her to use no more than $K$ ($1 \le K \le 3$) "PRINT" statements. Note that Bessie can use as many "REP" statements as she wants. Also note that each positive integer in the sequence is no greater than $K$.

For each of $T$ ($1 \le T \le 100$) independent test cases, determine whether Bessie can write a program that outputs some given sequence using at most $K$ "PRINT" statements.

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

The first line contains $T$.

The first line of each test case contains two space-separated integers, $N$ and $K$.

The second line of each test case contains a sequence of $N$ space-separated positive integers, each at most $K$, which is the sequence that Bessie wants to produce.

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

For each test case, output "YES" or "NO" (case sensitive) on a separate line.

SAMPLE INPUT:

2
1 1
1
4 1
1 1 1 1

SAMPLE OUTPUT:

YES
YES
For the second test case, the following code outputs the sequence $[1,1,1,1]$ with $1$ "PRINT" statement.

REP 4
    PRINT 1
END

SAMPLE INPUT:

11
4 2
1 2 2 2
4 2
1 1 2 1
4 2
1 1 2 2
6 2
1 1 2 2 1 1
10 2
1 1 1 2 2 1 1 1 2 2
8 3
3 3 1 2 2 1 2 2
9 3
1 1 2 2 2 3 3 3 3
16 3
2 2 3 2 2 3 1 1 2 2 3 2 2 3 1 1
24 3
1 1 2 2 3 3 3 2 2 3 3 3 1 1 2 2 3 3 3 2 2 3 3 3
9 3
1 2 2 1 3 3 1 2 2
6 3
1 2 1 2 2 3

SAMPLE OUTPUT:

YES
NO
YES
NO
YES
YES
YES
YES
YES
NO
NO
For the first test case, the following code outputs the sequence $[1,2,2,2]$ with $2$ "PRINT" statements.

PRINT 1
REP 3
    PRINT 2
END

For the second test case, the answer is "NO" because it is impossible to output the sequence $[1,1,2,1]$ using at most $2$ "PRINT" statements.

For the sixth test case, the following code outputs the sequence $[3,3,1,2,2,1,2,2]$ with $3$ "PRINT" statements.

REP 2
    PRINT 3
END
REP 2
    PRINT 1
    REP 2
        PRINT 2
    END
END

SCORING:

  • Input 3: $K=1$
  • Inputs 4-7: $K \le 2$
  • Inputs 8-13: No additional constraints.

Problem credits: Alex Liang

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 8.1k平均输出 token(含思考) ↓
  • 28.5输出速度 tokens/s ↑
  • 286 秒延迟 ↓
  • 7.55×CPU 时间 / 官方解 ↓
  • 不确定时间复杂度(相对官方解)
  • 6Pylint 问题 ↓
  • 8.85Pylint 得分 ↑
  • 28圈复杂度 ↓
  • 57.6可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

0/1

解出的采样 ↑

  • 92.3采样 1 · 超时
  • 7.8k平均输出 token(含思考) ↓
  • 191.5输出速度 tokens/s ↑
  • 41 秒延迟 ↓
  • 17.47×CPU 时间 / 官方解 ↓
  • 不确定时间复杂度(相对官方解)
  • 6Pylint 问题 ↓
  • 8.64Pylint 得分 ↑
  • 16圈复杂度 ↓
  • 60.6可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 21.9k平均输出 token(含思考) ↓
  • 155.4输出速度 tokens/s ↑
  • 141 秒延迟 ↓
  • 7.81×CPU 时间 / 官方解 ↓
  • 不确定时间复杂度(相对官方解)
  • 7Pylint 问题 ↓
  • 9.09Pylint 得分 ↑
  • 30圈复杂度 ↓
  • 53.3可维护性指数 ↑

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

代码

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


def can_make(a, k):
    n = len(a)

    if k == 1:
        return all(x == a[0] for x in a)
    if n <= k:
        return True

    inf = k + 1

    # lcp[i][j] = length of the longest common prefix of a[i:] and a[j:].
    lcp = [[0] * (n + 1) for _ in range(n + 1)]
    for i in range(n - 1, -1, -1):
        for j in range(n - 1, -1, -1):
            if a[i] == a[j]:
                lcp[i][j] = lcp[i + 1][j + 1] + 1

    # Proper base lengths that can be repeated to obtain each length.
    divisors = [[] for _ in range(n + 1)]
    for length in range(2, n + 1):
        divisors[length] = [
            d for d in range(1, length // 2 + 1) if length % d == 0
        ]

    # dp[l][r] = minimum PRINT statements needed to output a[l:r],
    # capped at k+1 when more than k are needed.
    dp = [[inf] * (n + 1) for _ in range(n)]
    for i in range(n):
        dp[i][i + 1] = 1

    for length in range(2, n + 1):
        for left in range(n - length + 1):
            right = left + length
            best = inf

            # Wrap a program for a shorter substring in REP, repeating it.
            for base_len in divisors[length]:
                if lcp[left][left + base_len] >= length - base_len:
                    best = min(best, dp[left][left + base_len])
                    if best == 1:
                        break

            # Concatenate programs for two consecutive substrings.
            if best != 1:
                for mid in range(left + 1, right):
                    cost = dp[left][mid] + dp[mid][right]
                    if cost < best:
                        best = cost
                        if best == 2:
                            break

            dp[left][right] = best

    return dp[0][n] <= k


def main():
    data = sys.stdin.buffer.read().split()
    if not data:
        return

    it = iter(data)
    t = int(next(it))
    answers = []

    for _ in range(t):
        n = int(next(it))
        k = int(next(it))
        a = [int(next(it)) for _ in range(n)]
        answers.append("YES" if can_make(a, k) else "NO")

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


if __name__ == "__main__":
    main()