mirror of
https://github.com/InternLM/InternBootcamp.git
synced 2026-04-23 16:55:02 +00:00
227 lines
6.6 KiB
Python
Executable file
227 lines
6.6 KiB
Python
Executable file
"""#
|
||
|
||
### 谜题描述
|
||
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as:
|
||
|
||
* a1 = p, where p is some integer;
|
||
* ai = ai - 1 + ( - 1)i + 1·q (i > 1), where q is some integer.
|
||
|
||
|
||
|
||
Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression.
|
||
|
||
Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements.
|
||
|
||
Input
|
||
|
||
The first line contains integer n (1 ≤ n ≤ 4000). The next line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106).
|
||
|
||
Output
|
||
|
||
Print a single integer — the length of the required longest subsequence.
|
||
|
||
Examples
|
||
|
||
Input
|
||
|
||
2
|
||
3 5
|
||
|
||
|
||
Output
|
||
|
||
2
|
||
|
||
|
||
Input
|
||
|
||
4
|
||
10 20 10 30
|
||
|
||
|
||
Output
|
||
|
||
3
|
||
|
||
Note
|
||
|
||
In the first test the sequence actually is the suitable subsequence.
|
||
|
||
In the second test the following subsequence fits: 10, 20, 10.
|
||
|
||
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
|
||
```python
|
||
import os, sys, atexit
|
||
range = xrange
|
||
from cStringIO import StringIO as BytesIO
|
||
sys.stdout = BytesIO()
|
||
atexit.register(lambda: os.write(1, sys.stdout.getvalue()))
|
||
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
|
||
from sys import stdin
|
||
from collections import *
|
||
|
||
rints = lambda: [int(x) for x in input().split()]
|
||
n, a, ans = int(input()), rints(), 0
|
||
mem, lst = [0] * (1000001), [-1] * (1000001)
|
||
|
||
for i in range(n):
|
||
for j in range(i, n):
|
||
if a[j] == a[i]:
|
||
mem[a[i]] += 1
|
||
elif lst[a[i]] >= lst[a[j]]:
|
||
mem[a[j]] += 2
|
||
|
||
lst[a[j]] = j
|
||
|
||
for j in range(i, n):
|
||
if lst[a[i]] > lst[a[j]]:
|
||
ans = max(ans, mem[a[j]] + 1)
|
||
else:
|
||
ans = max(ans, mem[a[j]])
|
||
if a[j] != a[i]:
|
||
mem[a[j]], lst[a[j]] = 0, -1
|
||
mem[a[i]], lst[a[i]] = 0, -1
|
||
|
||
print(ans)
|
||
```
|
||
|
||
|
||
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
|
||
"""
|
||
|
||
from bootcamp import Basebootcamp
|
||
import random
|
||
from collections import defaultdict
|
||
from bootcamp import Basebootcamp
|
||
|
||
class Aalmostarithmeticalprogressionbootcamp(Basebootcamp):
|
||
def __init__(self, **params):
|
||
super().__init__(**params)
|
||
self.max_n = min(params.get('max_n', 50), 4000)
|
||
self.min_val = max(params.get('min_val', 1), 1)
|
||
self.max_val = min(params.get('max_val', 10**6), 10**6)
|
||
|
||
def case_generator(self):
|
||
# 生成策略优化:覆盖边界情况及有效AAP结构
|
||
if random.random() < 0.3:
|
||
# 边界情况生成
|
||
return self._generate_edge_case()
|
||
else:
|
||
return self._generate_standard_case()
|
||
|
||
def _generate_edge_case(self):
|
||
"""生成边界测试用例(全相同元素、交替元素等)"""
|
||
case_type = random.choice([
|
||
'all_same',
|
||
'alternating',
|
||
'single_element'
|
||
])
|
||
|
||
if case_type == 'all_same':
|
||
n = random.randint(1, self.max_n)
|
||
val = random.randint(self.min_val, self.max_val)
|
||
return {
|
||
"n": n,
|
||
"b": [val]*n,
|
||
"ans": n
|
||
}
|
||
|
||
elif case_type == 'alternating':
|
||
n = random.randint(2, self.max_n)
|
||
a, b = random.sample(range(self.min_val, self.max_val+1), 2)
|
||
return {
|
||
"n": n,
|
||
"b": [a, b]*(n//2) + [a]*(n%2),
|
||
"ans": n
|
||
}
|
||
|
||
else: # single_element
|
||
return {
|
||
"n": 1,
|
||
"b": [random.randint(self.min_val, self.max_val)],
|
||
"ans": 1
|
||
}
|
||
|
||
def _generate_standard_case(self):
|
||
"""标准案例生成逻辑改进"""
|
||
# 构造有效AAP序列
|
||
base_len = random.randint(3, self.max_n)
|
||
aap = self._generate_valid_aap(base_len)
|
||
|
||
# 插入噪声元素
|
||
noise_num = random.randint(0, self.max_n - base_len)
|
||
b = self._insert_noise(aap, noise_num)
|
||
random.shuffle(b) # 保持子序列顺序但不要求连续
|
||
|
||
return {
|
||
"n": len(b),
|
||
"b": b,
|
||
"ans": self.calculate_max_aap_length(b)
|
||
}
|
||
|
||
def _generate_valid_aap(self, length):
|
||
"""生成符合AAP定义的基准序列"""
|
||
p = random.randint(self.min_val, self.max_val)
|
||
q = random.randint(1, (self.max_val - self.min_val)//2)
|
||
sequence = [p]
|
||
for i in range(1, length):
|
||
sign = (-1)**(i+1)
|
||
sequence.append(sequence[i-1] + sign * q)
|
||
return sequence
|
||
|
||
def _insert_noise(self, base, noise_num):
|
||
"""随机插入噪声元素"""
|
||
for _ in range(noise_num):
|
||
insert_pos = random.randint(0, len(base))
|
||
base.insert(insert_pos, random.randint(self.min_val, self.max_val))
|
||
return base
|
||
|
||
@staticmethod
|
||
def prompt_func(question_case) -> str:
|
||
n = question_case["n"]
|
||
b = " ".join(map(str, question_case["b"]))
|
||
return f"""Find the length of the longest subsequence that forms an almost arithmetical progression (AAP) where:
|
||
- a₁ is any integer
|
||
- For i > 1: aᵢ = aᵢ₋₁ + (-1)^(i+1)·q (q is integer)
|
||
|
||
Input:
|
||
{n}
|
||
{b}
|
||
|
||
Output format: Only the integer answer within [answer] tags, like:
|
||
[answer]4[/answer]"""
|
||
|
||
@staticmethod
|
||
def extract_output(output):
|
||
import re
|
||
matches = re.findall(r'\[answer\](.*?)\[/answer\]', output, re.DOTALL)
|
||
if not matches:
|
||
return None
|
||
try:
|
||
return int(matches[-1].strip().split()[0].replace(',', ''))
|
||
except:
|
||
return None
|
||
|
||
@classmethod
|
||
def _verify_correction(cls, solution, identity):
|
||
if not isinstance(solution, int) or solution < 1:
|
||
return False
|
||
return solution == identity["ans"]
|
||
|
||
@staticmethod
|
||
def calculate_max_aap_length(b):
|
||
"""精确实现原题解算法"""
|
||
n = len(b)
|
||
if n <= 1:
|
||
return n
|
||
|
||
max_len = 1
|
||
dp = defaultdict(lambda: defaultdict(int))
|
||
|
||
for i in range(n):
|
||
for j in range(i+1, n):
|
||
key = (b[i], b[j] - ((-1)**(2+1)) * (b[j] - b[i]))
|
||
dp[j][key] = max(dp[j].get(key, 0), dp[i].get(key, 1) + 1)
|
||
max_len = max(max_len, dp[j][key])
|
||
|
||
return max(max_len, 2 if n >=2 else 1)
|