题面
来源:USACO 官网。模型收到的是同一份题面的文字。
**Note: We suggest using a language other than Python to earn full credit on this problem.**
Farmer John's $N$ ($1 \leq N \leq 7500$) 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 of 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. For each $c=0 \ldots N$, help FJ find the number of distinct operations ($l,r$) that result in exactly $c$ cows being checked. Two operations ($l_1,r_1$) and ($l_2,r_2$) are different if $l_1 \neq l_2$ or $r_1 \neq r_2$.
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 $N+1$ lines with the $i$-th line containing the number of distinct operations ($l,r$) that result in $i-1$ cows being checked.SAMPLE INPUT:
3 1 3 2 3 2 1
SAMPLE OUTPUT:
3 3 0 0If 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 any of the cows' locations.
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.
SAMPLE INPUT:
3 1 2 3 1 2 3
SAMPLE OUTPUT:
0 3 0 3The three possible operations that cause $3$ cows to be checked are ($l=1,r=1$), ($l=2,r=2$), and ($l=3,r=3$).
SAMPLE INPUT:
7 1 3 2 2 1 3 2 3 2 2 1 2 3 1
SAMPLE OUTPUT:
0 6 14 6 2 0 0 0The two possible operations that cause $4$ cows to be checked are ($l=4,r=5$) and ($l=5,r=7$).
SCORING:
- Inputs 4-6: $N\le 100$
- Inputs 7-13: No additional constraints
Problem credits: Chongtian Ma and Haokai Ma
结果
Kimi K3.1 ProKimi
0/1
解出的采样 ↑
- 46.2采样 1 · 超时
- 11.9k平均输出 token(含思考) ↓
- 28.0输出速度 tokens/s ↑
- 426 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 2Pylint 问题 ↓
- 9.00Pylint 得分 ↑
- 4圈复杂度 ↓
- 74.1可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
0/1
解出的采样 ↑
- 46.2采样 1 · 超时
- 12.1k平均输出 token(含思考) ↓
- 200.5输出速度 tokens/s ↑
- 61 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 9.36Pylint 得分 ↑
- 10圈复杂度 ↓
- 54.8可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
0/1
解出的采样 ↑
- 84.6采样 1 · 超时
- 45.2k平均输出 token(含思考) ↓
- 145.1输出速度 tokens/s ↑
- 313 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 9.55Pylint 得分 ↑
- 11圈复杂度 ↓
- 62.3可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def main(): data = list(map(int, sys.stdin.buffer.read().split())) n = data[0] a = data[1:n + 1] b = data[n + 1:] original_match = [x == y for x, y in zip(a, b)] base = sum(original_match) answer = [0] * (n + 1) # All n operations with l == r leave the line unchanged. answer[base] = n # Operations with l < r are grouped by l + r. On each such diagonal, # successive operations expand the previous reversed interval by one cow # on each side, so only the two new endpoints change the match count. for diagonal in range(1, 2 * n - 2): left = (diagonal - 1) // 2 right = diagonal - left checked = base for l, r in zip(range(left, -1, -1), range(right, n)): checked += ( (a[r] == b[l]) + (a[l] == b[r]) - original_match[l] - original_match[r] ) answer[checked] += 1 sys.stdout.write("\n".join(map(str, answer)) + "\n") if __name__ == "__main__": main()
import sys def main(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return n = data[0] a = data[1:1 + n] b = data[1 + n:1 + 2 * n] orig = [0] * n matches = 0 for i in range(n): if a[i] == b[i]: orig[i] = 1 matches += 1 ans = [0] * (n + 1) # Reversing a length-1 interval changes nothing. ans[matches] += n aa = a bb = b oo = orig an = ans base = matches # Odd-length intervals with length >= 3, centered at c. for c in range(1, n - 1): right = n - 1 - c steps = c if c < right else right l = c - 1 r = c + 1 cur = 0 for _ in range(steps): cur += ( (aa[r] == bb[l]) + (aa[l] == bb[r]) - oo[l] - oo[r] ) an[base + cur] += 1 l -= 1 r += 1 # Even-length intervals, centered between c and c + 1. for c in range(n - 1): left_steps = c + 1 right_steps = n - c - 1 steps = left_steps if left_steps < right_steps else right_steps l = c r = c + 1 cur = 0 for _ in range(steps): cur += ( (aa[r] == bb[l]) + (aa[l] == bb[r]) - oo[l] - oo[r] ) an[base + cur] += 1 l -= 1 r += 1 sys.stdout.write("\n".join(map(str, ans)) + "\n") if __name__ == "__main__": main()
import sys from array import array from itertools import accumulate from struct import pack as struct_pack try: # Fast C implementation used by collections.Counter.update. from _collections import _count_elements except ImportError: def _count_elements(mapping, iterable): get = mapping.get for key in iterable: mapping[key] = get(key, 0) + 1 # Maps an unsigned value 0,1,2,3,4 to a signed byte with value -2,-1,0,1,2. SIGNED_TABLE = bytes((254, 255, 0, 1, 2)) + bytes(251) 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] same = [x == y for x, y in zip(a, b)] base = sum(same) mismatch = [0 if x else 1 for x in same] def pack_fields(values): """Pack integers as little-endian unsigned 16-bit fields.""" return struct_pack(f"<{len(values)}H", *values) a_view = memoryview(pack_fields(a)) b_view = memoryview(pack_fields(b)) a_rev_view = memoryview(pack_fields(a[::-1])) b_rev_view = memoryview(pack_fields(b[::-1])) mismatch_view = memoryview(pack_fields(mismatch)) mismatch_rev_view = memoryview(pack_fields(mismatch[::-1])) # A 1 in the lowest bit of every 16-bit field. field_ones = ((1 << (16 * n)) - 1) // 65535 all_low = field_ones * 0x7FFF all_high = field_ones << 15 def segment(view, start, count): """Extract count consecutive 16-bit fields.""" return int.from_bytes( view[2 * start:2 * (start + count)], "little" ) def equal_fields(x, y, low, high): """ Return one bit per 16-bit field, indicating whether x and y are equal in that field. """ different = ((x ^ y) + low) & high return (high ^ different) >> 15 def make_deltas(k, left_rev_start, right_start): """ Return signed changes for k symmetric endpoint pairs. The left endpoints are read in decreasing order from the reversed buffers, and the right endpoints in increasing order. """ mask = (1 << (16 * k)) - 1 low = all_low & mask high = all_high & mask # [a[right] == b[left]] equal1 = equal_fields( segment(a_view, right_start, k), segment(b_rev_view, left_rev_start, k), low, high, ) # [a[left] == b[right]] equal2 = equal_fields( segment(a_rev_view, left_rev_start, k), segment(b_view, right_start, k), low, high, ) left_mismatch = segment( mismatch_rev_view, left_rev_start, k ) right_mismatch = segment( mismatch_view, right_start, k ) # In each field: # delta + 2 = equal1 + equal2 + mismatch_left + mismatch_right. packed = ( equal1 + equal2 + left_mismatch + right_mismatch ) # Every field is between 0 and 4, so its high byte is zero. encoded = packed.to_bytes(2 * k, "little")[::2] delta = array("b") delta.frombytes(encoded.translate(SIGNED_TABLE)) return delta counts = {} # Odd-length intervals: [center - radius, center + radius]. for center in range(n): k = min(center, n - 1 - center) if k == 0: # The singleton interval leaves the entire line unchanged. counts[base] = counts.get(base, 0) + 1 continue delta = make_deltas(k, n - center, center + 1) # The initial value accounts for the singleton interval. _count_elements(counts, accumulate(delta, initial=base)) # Even-length intervals: # [gap - 1 - radius, gap + radius]. for gap in range(1, n): k = min(gap, n - gap) delta = make_deltas(k, n - gap, gap) # The initial value is a placeholder for this center. _count_elements(counts, accumulate(delta, initial=base)) # Remove the placeholder inserted for each of the n - 1 even centers. counts[base] = counts.get(base, 0) - (n - 1) sys.stdout.write( "\n".join(str(counts.get(c, 0)) for c in range(n + 1)) + "\n" ) if __name__ == "__main__": solve()