InternBootcamp/internbootcamp/bootcamp/crotationmatching/crotationmatching.py
2025-05-23 15:27:15 +08:00

238 lines
6.6 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""#
### 谜题描述
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size n. Let's call them a and b.
Note that a permutation of n elements is a sequence of numbers a_1, a_2, …, a_n, in which every number from 1 to n appears exactly once.
The message can be decoded by an arrangement of sequence a and b, such that the number of matching pairs of elements between them is maximum. A pair of elements a_i and b_j is said to match if:
* i = j, that is, they are at the same index.
* a_i = b_j
His two disciples are allowed to perform the following operation any number of times:
* choose a number k and cyclically shift one of the permutations to the left or right k times.
A single cyclic shift to the left on any permutation c is an operation that sets c_1:=c_2, c_2:=c_3, …, c_n:=c_1 simultaneously. Likewise, a single cyclic shift to the right on any permutation c is an operation that sets c_1:=c_n, c_2:=c_1, …, c_n:=c_{n-1} simultaneously.
Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the size of the arrays.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — the elements of the first permutation.
The third line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ n) — the elements of the second permutation.
Output
Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.
Examples
Input
5
1 2 3 4 5
2 3 4 5 1
Output
5
Input
5
5 4 3 2 1
1 2 3 4 5
Output
1
Input
4
1 3 2 4
4 2 3 1
Output
2
Note
For the first case: b can be shifted to the right by k = 1. The resulting permutations will be \{1, 2, 3, 4, 5\} and \{1, 2, 3, 4, 5\}.
For the second case: The operation is not required. For all possible rotations of a and b, the number of matching pairs won't exceed 1.
For the third case: b can be shifted to the left by k = 1. The resulting permutations will be \{1, 3, 2, 4\} and \{2, 3, 1, 4\}. Positions 2 and 4 have matching pairs of elements. For all possible rotations of a and b, the number of matching pairs won't exceed 2.
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
```python
from collections import defaultdict, Counter
from math import log, ceil
import sys
#sys.stdin = open(\"third.txt\", \"r\")
raw_input = sys.stdin.readline
def rotateL(arr, d, n):
tmp = [0 for n_itr in xrange(n)]
for n_itr in xrange(n):
tmp[(n_itr + d) % n] = arr[n_itr]
return tmp
def rotateR(arr, d, n):
tmp = [0 for n_itr in xrange(n)]
for n_itr in xrange(n):
tmp[(n_itr - d) % n] = arr[n_itr]
return tmp
n = int(raw_input())
a = map(int, raw_input().split())
b = map(int, raw_input().split())
p1 = {}
p2 = {}
for n_itr in xrange(n):
p1[a[n_itr]] = n_itr
p2[b[n_itr]] = n_itr
p = defaultdict(int)
for number in a:
tmp = p1[number] - p2[number]
tmp += (0 if tmp > 0 else n)
p[tmp] += 1
ma = 0
for key in p:
if p[key] > ma:
ma = p[key]
print ma
```
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
"""
from bootcamp import Basebootcamp
import random
import re
from collections import defaultdict
from bootcamp import Basebootcamp
class Crotationmatchingbootcamp(Basebootcamp):
def __init__(self, n_min=3, n_max=10, rotation_prob=0.5):
self.n_min = n_min
self.n_max = n_max
self.rotation_prob = rotation_prob
def case_generator(self):
n = random.randint(self.n_min, self.n_max)
a = list(range(1, n + 1))
random.shuffle(a)
if random.random() < self.rotation_prob:
shift = random.randint(0, n - 1)
b = a[shift:] + a[:shift]
else:
if random.random() < 0.5:
b = a[::-1]
else:
b = list(range(1, n + 1))
random.shuffle(b)
return {
'n': n,
'a': a,
'b': b
}
@staticmethod
def prompt_func(question_case) -> str:
n = question_case['n']
a_str = ' '.join(map(str, question_case['a']))
b_str = ' '.join(map(str, question_case['b']))
return f"""You are a puzzle solver. Help Ishika and Hriday find the maximum number of matching pairs after performing any number of cyclic shifts on either permutation.
**Problem Description:**
After the mysterious disappearance of Ashish, his two disciples were left with permutations a and b. They can cyclically shift their respective permutations any number of times. A matching pair occurs when elements at the same index are equal. Your task is to determine the maximum possible number of such pairs.
**Operations Allowed:**
- Cyclic left shift: Each element moves to the previous index, and the first element wraps to the end.
- Cyclic right shift: Each element moves to the next index, and the last element wraps to the start.
**Input Format:**
- The first line contains an integer n (1 ≤ n ≤ 2×10^5), the size of the permutations.
- The second line contains permutation a.
- The third line contains permutation b.
**Output Format:**
A single integer indicating the maximum number of matching pairs.
**Example Input:**
5
1 2 3 4 5
2 3 4 5 1
**Example Output:**
5
**Your Task:**
n: {n}
a: {a_str}
b: {b_str}
Please provide your answer as a single integer enclosed within [answer] and [/answer] tags. For example: [answer]5[/answer]. Ensure your final answer is the last one provided."""
@staticmethod
def extract_output(output):
matches = re.findall(r'\[answer\](.*?)\[/answer\]', output, re.DOTALL)
if not matches:
return None
last_match = matches[-1].strip()
try:
return int(last_match)
except ValueError:
return None
@classmethod
def _verify_correction(cls, solution, identity):
n = identity['n']
a = identity['a']
b = identity['b']
if n == 0:
return solution == 0
p1 = {x: idx for idx, x in enumerate(a)}
p2 = {x: idx for idx, x in enumerate(b)}
delta_counts = defaultdict(int)
for x in a:
delta = (p1[x] - p2[x]) % n
delta_counts[delta] += 1
max_count = max(delta_counts.values()) if delta_counts else 0
return solution == max_count