题面
来源:USACO 官网。模型收到的是同一份题面的文字。
The cows are in a particularly mischievous mood today! All Farmer John wants to do is take a photograph of the cows standing in a line, but they keep moving right before he has a chance to snap the picture.
Specifically, each of FJ's $N$ cows $(1 \le N \le 10^5)$ has an integer height from $1$ to $N$. FJ wants to take a picture of the cows standing in line in a very specific ordering. If the cows have heights $h_1, \dots, h_K$ when lined up from left to right, he wants the cow heights to have the following three properties:
- He wants the cow heights to increase and then decrease. Formally, there must exist an integer $i$ such that $h_1 \le \dots \le h_i \ge \dots \ge h_K$.
- He does not want any cow standing next to another cow with exactly the same height. Formally, $h_i \neq h_{i+1}$ for all $1 \le i < K$.
- He wants the picture to be symmetric. Formally, if $i + j = K+1$, then $h_i = h_j$.
FJ wants the picture to contain as many cows as possible. Specifically, FJ can remove some cows and rearrange the remaining ones. Compute the maximum number of cows FJ can have in the picture satisfying his constraints.
INPUT FORMAT (input arrives from the terminal / stdin):
You have to answer multiple test cases.The first line of input contains a single integer $T$ ($1 \leq T \leq 10^5$) denoting the number of test cases. $T$ test cases follow.
The first line of every test case contains a single integer $N$. The second line of every test case contains $N$ integers, the heights of the $N$ cows available. The cow heights will be between $1$ and $N$.
It is guaranteed the sum of $N$ over all test cases will not exceed $10^6$.
OUTPUT FORMAT (print output to the terminal / stdout):
Output $T$ lines, the $i$'th line containing the answer to the $i$'th test case. Each line should be an integer denoting the maximum number of cows FJ can include in the picture.SAMPLE INPUT:
2 4 1 1 2 3 4 3 3 2 1
SAMPLE OUTPUT:
3 1
For the first test case, FJ can take the cows with heights $1$, $1$, and $3$, and rearrange them into $[1,3,1]$, which satisfies all the conditions. For the second test case, FJ can take the cow with height $3$ and form a valid photo.
SCORING:
- Inputs 2-3: $T\le 100, N \le 7$
- Inputs 4-5: $T \le 10^4$, all cows will have height at most 10.
- Inputs 6-11: No additional constraints.
Problem credits: Nick Wu
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 3.8k平均输出 token(含思考) ↓
- 30.7输出速度 tokens/s ↑
- 125 秒延迟 ↓
- 0.57×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 8.89Pylint 得分 ↑
- 6圈复杂度 ↓
- 70.0可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 2.7k平均输出 token(含思考) ↓
- 191.0输出速度 tokens/s ↑
- 15 秒延迟 ↓
- 0.67×CPU 时间 / 官方解 ↓
- 不确定时间复杂度(相对官方解)
- 2Pylint 问题 ↓
- 9.17Pylint 得分 ↑
- 6圈复杂度 ↓
- 65.7可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 4.6k平均输出 token(含思考) ↓
- 174.1输出速度 tokens/s ↑
- 27 秒延迟 ↓
- 0.56×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 8.80Pylint 得分 ↑
- 7圈复杂度 ↓
- 72.6可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def main(): data = sys.stdin.buffer.read().split() t = int(data[0]) pos = 1 answers = [] for _ in range(t): n = int(data[pos]) pos += 1 counts = [0] * (n + 1) paired_heights = 0 max_height = 0 for _ in range(n): h = int(data[pos]) pos += 1 counts[h] += 1 if counts[h] == 2: paired_heights += 1 if h > max_height: max_height = h # Each paired height contributes two cows. If the maximum height # is unique, it can be the center while retaining every pair; # otherwise, one paired height must be sacrificed as the center. if counts[max_height] == 1: answer = 2 * paired_heights + 1 else: answer = 2 * paired_heights - 1 answers.append(str(answer)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()
import sys def main(): data = list(map(int, sys.stdin.buffer.read().split())) t = data[0] idx = 1 answers = [] for _ in range(t): n = data[idx] idx += 1 freq = [0] * (n + 1) for _ in range(n): freq[data[idx]] += 1 idx += 1 eligible = 0 # number of smaller heights with frequency at least 2 best = 1 for h in range(1, n + 1): if freq[h] > 0: best = max(best, 2 * eligible + 1) if freq[h] >= 2: eligible += 1 answers.append(str(best)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()
import sys def solve(): data = map(int, sys.stdin.buffer.read().split()) t = next(data) answers = [] for _ in range(t): n = next(data) freq = [0] * (n + 1) for _ in range(n): freq[next(data)] += 1 paired_heights = 0 best = 1 for height in range(1, n + 1): count = freq[height] if count == 0: continue # Use this height as the unique central peak. candidate = 1 + 2 * paired_heights if candidate > best: best = candidate # This height may be used as a symmetric pair only # for a strictly higher peak. if count >= 2: paired_heights += 1 answers.append(str(best)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": solve()