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

223 lines
5.8 KiB
Python
Executable file

"""#
### 谜题描述
You are given an array a of n integers, where n is odd. You can make the following operation with it:
* Choose one of the elements of the array (for example a_i) and increase it by 1 (that is, replace it with a_i + 1).
You want to make the median of the array the largest possible using at most k operations.
The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array [1, 5, 2, 3, 5] is 3.
Input
The first line contains two integers n and k (1 ≤ n ≤ 2 ⋅ 10^5, n is odd, 1 ≤ k ≤ 10^9) — the number of elements in the array and the largest number of operations you can make.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9).
Output
Print a single integer — the maximum possible median after the operations.
Examples
Input
3 2
1 3 5
Output
5
Input
5 5
1 2 1 1 1
Output
3
Input
7 7
4 1 2 4 3 4 4
Output
5
Note
In the first example, you can increase the second element twice. Than array will be [1, 5, 5] and it's median is 5.
In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is 3.
In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be [5, 1, 2, 5, 3, 5, 5] and the median will be 5.
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
```python
import fileinput
def D(a):print(a)
def S(s,I):return int(s.split(\" \")[I])
def ok(t):
global A
global N
global K
S=0
for i in xrange(N/2,N):
S+=max(t-A[i],0)
return S>K
def main():
z=0
global A
global N
global K
for l in fileinput.input():
z+=1
if(z<2):
N=S(l,0)
K=S(l,1)
continue
A=map(int,l.split(\" \"))
A.sort()
M=0
B=0
E=2000000666
while(B<E):
M=(B+E)/2
if(ok(M)):E=M
else: B=M+1
D(B-1)
main()
```
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
"""
from bootcamp import Basebootcamp
import random
import re
from bootcamp import Basebootcamp
class Cmaximummedianbootcamp(Basebootcamp):
def __init__(self, **params):
# Set default parameters
self.min_n = params.get('min_n', 3)
self.max_n = params.get('max_n', 9)
self.min_k = params.get('min_k', 1)
self.max_k = params.get('max_k', 500)
self.min_val = params.get('min_val', 1)
self.max_val = params.get('max_val', 100)
# Ensure n parameters are valid odd numbers
if self.min_n % 2 == 0:
self.min_n += 1
if self.max_n % 2 == 0:
self.max_n -= 1
self.min_n = max(1, self.min_n)
self.max_n = max(self.min_n, self.max_n)
def case_generator(self):
# Generate n as an odd number within the specified range
possible_n = list(range(self.min_n, self.max_n + 1, 2))
n = random.choice(possible_n)
# Generate array elements within the specified value range
array = [random.randint(self.min_val, self.max_val) for _ in range(n)]
# Generate k within the specified range
k = random.randint(self.min_k, self.max_k)
return {
'n': n,
'k': k,
'array': array.copy()
}
@staticmethod
def prompt_func(question_case) -> str:
n = question_case['n']
k = question_case['k']
array = question_case['array']
array_str = ' '.join(map(str, array))
examples = [
{
'input': "3 2\n1 3 5",
'output': 5
},
{
'input': "5 5\n1 2 1 1 1",
'output': 3
}
]
example_text = '\n'.join([f"Input:\n{ex['input']}\nOutput:\n{ex['output']}" for ex in examples])
prompt = f'''You are a programming competition contestant. Solve the following problem:
You are given an array of integers with an odd number of elements. You can perform up to k operations where each operation increases an element by 1. Your goal is to maximize the median of the array. The median is the middle element after sorting the array.
Input format:
- The first line contains two integers n and k. n is odd.
- The second line contains n integers representing the array.
Output:
- A single integer: the maximum possible median.
Examples:
{example_text}
Now, solve the following problem:
Input:
{n} {k}
{array_str}
What is the maximum possible median? Put your final answer within [answer] and [/answer] tags. For example: [answer]5[/answer].'''
return prompt
@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']
k_val = identity['k']
array = identity['array']
sorted_arr = sorted(array)
left = sorted_arr[n // 2]
right = left + k_val # Upper bound based on maximum possible increment
best = left
while left <= right:
mid = (left + right) // 2
required = 0
for i in range(n // 2, n):
if sorted_arr[i] < mid:
required += mid - sorted_arr[i]
if required > k_val:
break
if required <= k_val:
best = mid
left = mid + 1
else:
right = mid - 1
return solution == best