题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Farmer John's $N$ ($1 \leq N \leq 5 \cdot 10^5$) cows are standing in a line, with cow $1$ at the front of the line and cow $N$ at the back of the line. FJ's cows also come in many different species. He denotes each species with an integer from $1$ to $N$. The $i$'th cow from the front of the line is of species $a_i$ ($1 \leq a_i \leq N$).
FJ is taking his cows to a checkup at a local bovine hospital. However, the bovine veterinarian is very picky and wants to perform a checkup on the $i$'th cow in the line, only if it is species $b_i$ ($1 \leq b_i \leq N$).
FJ is lazy and does not want to completely reorder his cows. He will perform the following operation exactly once.
- Select two integers $l$ and $r$ such that $1 \leq l \le r \leq N$. Reverse the order of the cows that are between the $l$-th cow and the $r$-th cow in the line, inclusive.
FJ wants to measure how effective this approach is. Find the sum of the number of cows that are checked by the veterinarian over all $N(N+1)/2$ possible operations.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains an integer $N$.The second line contains $a_1, a_2, \ldots, a_N$.
The third line contains $b_1, b_2, \ldots, b_N$.
OUTPUT FORMAT (print output to the terminal / stdout):
Output one line with the sum of the number of cows that are checked by the veterinarian over all possible operations.SAMPLE INPUT:
3 1 3 2 3 2 1
SAMPLE OUTPUT:
3
If FJ chooses ($l=1,r=1$), ($l=2,r=2$), or ($l=3,r=3$) then no cows will be checked. Note that those operations do not modify the location of the cows.
The following operations result in one cow being checked.
- $l=1,r=2$: FJ reverses the order of the first and second cows so the species of each cow in the new lineup will be $[3,1,2]$. The first cow will be checked.
- $l=2,r=3$: FJ reverses the order of the second and third cows so the species of each cow in the new lineup will be $[1,2,3]$. The second cow will be checked.
- $l=1,r=3$: FJ reverses the order of the first, second, and third cows so the species of each cow in the new lineup will be $[2,3,1]$. The third cow will be checked.
The total number of cows checked over all six operations is $0+0+0+1+1+1=3$.
SAMPLE INPUT:
3 1 2 3 1 2 3
SAMPLE OUTPUT:
12
There are three possible operations that cause $3$ cows to be checked: ($l=1,r=1$), ($l=2,r=2$), and ($l=3,r=3$). The remaining operations each result in $1$ cow being checked. The total number of cows checked over all six operations is $3+3+3+1+1+1=12$.
SAMPLE INPUT:
7 1 3 2 2 1 3 2 3 2 2 1 2 3 1
SAMPLE OUTPUT:
60
SCORING:
- Input 4: $N\le 100$
- Input 5: $N\le 5000$
- Inputs 6-9: $a_i, b_i$ are all generated uniformly at random in the range $[1,N]$
- Inputs 10-15: $a_i, b_i$ are all generated uniformly at random in the range $[1,2]$
- Inputs 16-23: No additional constraints.
Problem credits: Chongtian Ma, Haokai Ma, and Alex Liang
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 7.2k平均输出 token(含思考) ↓
- 32.7输出速度 tokens/s ↑
- 221 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 9.35Pylint 得分 ↑
- 7圈复杂度 ↓
- 59.1可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 15.2k平均输出 token(含思考) ↓
- 216.8输出速度 tokens/s ↑
- 71 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 7Pylint 问题 ↓
- 8.85Pylint 得分 ↑
- 12圈复杂度 ↓
- 57.5可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 13.9k平均输出 token(含思考) ↓
- 185.1输出速度 tokens/s ↑
- 76 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 9.29Pylint 得分 ↑
- 9圈复杂度 ↓
- 61.4可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def solve(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return n = data[0] a = data[1:n + 1] b = data[n + 1:n + 1 + 2 * n] del data total_operations = n * (n + 1) // 2 answer = 0 count_a = [0] * (n + 1) count_b = [0] * (n + 1) # Contributions from cows outside the reversed interval. for i in range(n): x = a[i] y = b[i] if x == y: position = i + 1 answer += total_operations - position * (n - position + 1) count_a[x] += 1 count_b[y] += 1 # current is the number of equal-species pairs (i in b, j in a) # inside the current interval [t, n + 1 - t]. current = 0 for species in range(1, n + 1): current += count_a[species] * count_b[species] last_t = (n + 1) // 2 for t in range(1, last_t + 1): answer += current if t == last_t: break # Shrink the interval by removing its two endpoints. left = t - 1 right = n - t x = a[left] current -= count_b[x] count_a[x] -= 1 y = b[left] current -= count_a[y] count_b[y] -= 1 x = a[right] current -= count_b[x] count_a[x] -= 1 y = b[right] current -= count_a[y] count_b[y] -= 1 sys.stdout.write(str(answer) + "\n") if __name__ == "__main__": solve()
import sys from bisect import bisect_right def main() -> None: input = sys.stdin.buffer.readline line = input() if not line: return N = int(line) a = list(map(int, input().split())) b = list(map(int, input().split())) # Group positions of a by species in a flat sorted-by-species array. count = [0] * (N + 2) for x in a: count[x] += 1 start = [0] * (N + 2) total = 0 for v in range(1, N + 1): start[v] = total total += count[v] start[N + 1] = total cursor = start[:] pos = [0] * N for i, x in enumerate(a, 1): p = cursor[x] pos[p] = i cursor[x] = p + 1 del count, cursor # Prefix sums over the flat position array. pref = [0] * (N + 1) acc = 0 for idx, p in enumerate(pos): acc += p pref[idx + 1] = acc num_operations = N * (N + 1) // 2 # Contributions from positions that are not reversed. total_matches = 0 removed = 0 for i in range(1, N + 1): if a[i - 1] == b[i - 1]: total_matches += 1 removed += i * (N - i + 1) answer = total_matches * num_operations - removed # Contributions from positions inside the reversed segment. N1 = N + 1 for i, species in enumerate(b, 1): lo = start[species] hi = start[species + 1] if lo == hi: continue other = N1 - i if i < other: t1 = i tmax = other else: t1 = other tmax = i idx1 = bisect_right(pos, t1, lo, hi) idx2 = bisect_right(pos, tmax, idx1, hi) # j <= t1: contribution is j answer += pref[idx1] - pref[lo] # t1 < j <= tmax: contribution is t1 answer += t1 * (idx2 - idx1) # j > tmax: contribution is N + 1 - j cnt = hi - idx2 if cnt: answer += N1 * cnt - (pref[hi] - pref[idx2]) sys.stdout.write(str(answer)) if __name__ == "__main__": main()
import sys def solve(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return n = data[0] a = data[1:n + 1] b = data[n + 1:2 * n + 1] del data # Initially every position is active. active_count = [0] * (n + 1) total_depth = [0] * (n + 1) for i, species in enumerate(a): depth = i + 1 if n - i < depth: depth = n - i active_count[species] += 1 total_depth[species] += depth active_sum = total_depth.copy() total_intervals = n * (n + 1) // 2 answer = 0 # The two positions of depth k are k-1 and n-k (0-based). for k in range(1, (n + 1) // 2 + 1): left = k - 1 right = n - k # Contributions for the left position. species = b[left] answer += ( total_depth[species] - active_sum[species] + k * active_count[species] ) # Reversals that do not contain this position. if a[left] == species: answer += total_intervals - (left + 1) * (n - left) # Contributions for the right position. if right != left: species = b[right] answer += ( total_depth[species] - active_sum[species] + k * active_count[species] ) if a[right] == species: answer += total_intervals - (right + 1) * (n - right) # Remove all positions whose depth is exactly k. species = a[left] active_count[species] -= 1 active_sum[species] -= k if right != left: species = a[right] active_count[species] -= 1 active_sum[species] -= k print(answer) if __name__ == "__main__": solve()