"""# ### 谜题描述 A sequence (b_1, b_2, …, b_k) is called strange, if the absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence. Formally speaking, it's strange if for every pair (i, j) with 1 ≤ i 0: print(1) else: min_diff = 10 ** 9 + 10 for i in range(1, n): if a[i] > 0: print i + 1 if min_diff >= a[i] else i break diff = a[i] - a[i - 1] min_diff = min(min_diff, diff) else: print(n) ``` 请完成上述谜题的训练场环境类实现,包括所有必要的方法。 """ from bootcamp import Basebootcamp import random import re from bootcamp import Basebootcamp class Bsifidandstrangesubsequencesbootcamp(Basebootcamp): def __init__(self, min_n=1, max_n=20, element_min=-100, element_max=100, special_cases_ratio=0.3): self.min_n = min_n self.max_n = max_n self.element_min = element_min self.element_max = element_max self.special_cases_ratio = special_cases_ratio def case_generator(self): if random.random() < self.special_cases_ratio: return self._generate_special_case() n = random.randint(self.min_n, self.max_n) a = [random.randint(self.element_min, self.element_max) for _ in range(n)] expected = self._compute_expected(a) return {'a': a, 'expected': expected} def _generate_special_case(self): case_type = random.choice([ 'all_positive', 'all_negative', 'mixed_signs', 'single_element', 'with_zero_edge' ]) n = random.randint(self.min_n, self.max_n) if case_type == 'all_positive': a = [random.randint(1, self.element_max) for _ in range(n)] elif case_type == 'all_negative': a = [random.randint(self.element_min, -1) for _ in range(n)] elif case_type == 'mixed_signs': a = [random.choice([-1, 1]) * random.randint(0, self.element_max) for _ in range(n)] elif case_type == 'single_element': a = [random.randint(self.element_min, self.element_max)] else: # with_zero_edge a = [0] + sorted([random.randint(-10, 10) for _ in range(n-1)]) expected = self._compute_expected(a) return {'a': a, 'expected': expected} @staticmethod def prompt_func(question_case) -> str: a = question_case['a'] return f"""You are solving a "strange subsequence" problem. **Formal Requirements:** Find the maximum length of a subsequence where: ∀i < j, |a_i - a_j| ≥ MAX (the maximum in the subsequence) **Key Insights:** 1. All non-positive elements can always be selected together 2. Can add at most one positive element (which must satisfy min_diff ≥ that value) **Input Array (n = {len(a)}):** {' '.join(map(str, a))} **Output Format:** A single integer in [answer]...[/answer] tags. Example: [answer]3[/answer]""" @staticmethod def extract_output(output): matches = re.findall(r'\[answer\s*](.*?)\[/answer\s*]', output, re.DOTALL | re.IGNORECASE) if not matches: return None try: return int(matches[-1].strip()) except (ValueError, TypeError): return None @classmethod def _verify_correction(cls, solution, identity): return isinstance(solution, int) and solution == identity['expected'] @staticmethod def _compute_expected(a): a_sorted = sorted(a) n = len(a_sorted) if a_sorted[0] > 0: return 1 min_diff = float('inf') for i in range(1, n): diff = a_sorted[i] - a_sorted[i-1] min_diff = min(min_diff, diff) if a_sorted[i] > 0: return i+1 if min_diff >= a_sorted[i] else i return n