题面
来源: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 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 YESFor 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 NOFor 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
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
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
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
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()
import sys def can_make(seq, K): n = len(seq) INF = 10**9 # dp[l][r] = minimum number of PRINT statements needed # to output seq[l:r]. dp = [[0] * (n + 1) for _ in range(n + 1)] for i in range(n): dp[i][i + 1] = 1 for length in range(2, n + 1): for l in range(n - length + 1): r = l + length best = INF # Concatenate two programs. for mid in range(l + 1, r): val = dp[l][mid] + dp[mid][r] if val < best: best = val # Repeat a smaller program. for p in range(1, length): if length % p != 0: continue if dp[l][l + p] >= best: continue ok = True for i in range(l + p, r): if seq[i] != seq[i - p]: ok = False break if ok: best = dp[l][l + p] dp[l][r] = best return dp[0][n] <= K def main(): data = list(map(int, sys.stdin.buffer.read().split())) t = data[0] idx = 1 answers = [] for _ in range(t): n = data[idx] k = data[idx + 1] idx += 2 seq = data[idx:idx + n] idx += n answers.append("YES" if can_make(seq, k) else "NO") sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()
import sys def build_proper_divisors(limit): """divisors[length] contains every proper divisor of length.""" divisors = [[] for _ in range(limit + 1)] for d in range(1, limit // 2 + 1): for length in range(2 * d, limit + 1, d): divisors[length].append(d) return divisors def can_generate(sequence, k, proper_divisors): n = len(sequence) # With one PRINT statement, the output must be constant. if k == 1: first = sequence[0] return all(value == first for value in sequence) INF = k + 1 # run_end[i] is the exclusive end of the constant run starting at i. run_end = [n] * n for i in range(n - 2, -1, -1): if sequence[i] == sequence[i + 1]: run_end[i] = run_end[i + 1] else: run_end[i] = i + 1 # lcp[i][j] = longest common prefix of sequence[i:] and sequence[j:]. # Only entries with i < j are needed. lcp = [[0] * (n + 1) for _ in range(n + 1)] for i in range(n - 1, -1, -1): current = lcp[i] following = lcp[i + 1] value = sequence[i] for j in range(n - 1, i, -1): if value == sequence[j]: current[j] = following[j + 1] + 1 # dp[left][right] is the minimum number of PRINT statements needed # for sequence[left:right], capped at INF. dp = [[INF] * (n + 1) for _ in range(n + 1)] for length in range(1, n + 1): divisors = proper_divisors[length] for left in range(n - length + 1): right = left + length row = dp[left] # REP length PRINT value generates a constant sequence. if right <= run_end[left]: row[right] = 1 continue best = INF lcp_row = lcp[left] # The sequence may be repetitions of a shorter program. for d in divisors: prefix_cost = row[left + d] if prefix_cost < best and lcp_row[left + d] >= length - d: best = prefix_cost # A nonconstant sequence requires at least two PRINTs. if best == 2: break # The sequence may be the concatenation of two programs. if best > 2: for middle in range(left + 1, right): cost = row[middle] + dp[middle][right] if cost < best: best = cost if best == 2: break row[right] = best return dp[0][n] <= k def main(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return t = data[0] position = 1 tests = [] max_n = 0 for _ in range(t): n = data[position] k = data[position + 1] position += 2 sequence = data[position:position + n] position += n tests.append((sequence, k)) max_n = max(max_n, n) proper_divisors = build_proper_divisors(max_n) answers = [] for sequence, k in tests: if can_generate(sequence, k, proper_divisors): answers.append("YES") else: answers.append("NO") sys.stdout.write("\n".join(answers) + "\n") if __name__ == "__main__": main()