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

151 lines
5.7 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.

"""#
### 谜题描述
Petya is preparing for his birthday. He decided that there would be n different dishes on the dinner table, numbered from 1 to n. Since Petya doesn't like to cook, he wants to order these dishes in restaurants.
Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from n different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it:
* the dish will be delivered by a courier from the restaurant i, in this case the courier will arrive in a_i minutes,
* Petya goes to the restaurant i on his own and picks up the dish, he will spend b_i minutes on this.
Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.
For example, if Petya wants to order n = 4 dishes and a = [3, 7, 4, 5], and b = [2, 1, 2, 4], then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in 3 minutes, the courier of the fourth restaurant will bring the order in 5 minutes, and Petya will pick up the remaining dishes in 1 + 2 = 3 minutes. Thus, in 5 minutes all the dishes will be at Petya's house.
Find the minimum time after which all the dishes can be at Petya's home.
Input
The first line contains one positive integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases. Then t test cases follow.
Each test case begins with a line containing one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of dishes that Petya wants to order.
The second line of each test case contains n integers a_1 … a_n (1 ≤ a_i ≤ 10^9) — the time of courier delivery of the dish with the number i.
The third line of each test case contains n integers b_1 … b_n (1 ≤ b_i ≤ 10^9) — the time during which Petya will pick up the dish with the number i.
The sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case output one integer — the minimum time after which all dishes can be at Petya's home.
Example
Input
4
4
3 7 4 5
2 1 2 4
4
1 2 3 4
3 3 3 3
2
1 2
10 10
2
10 10
1 2
Output
5
3
2
3
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
```python
def func(a,b):
return cmp(a[0],b[0])
for _ in range(input()):
n=input()
a=[int(i) for i in raw_input().split()]
b=[int(i) for i in raw_input().split()]
c=[[a[i],b[i]]for i in range(n)]
c=sorted(c,cmp=func)
dp=[0 for i in range(n+1)]
for i in range(1,n+1):
dp[i]+=dp[i-1]+c[i-1][1]
minn=dp[-1]
ct=0
for i in range(n):
minn=min(minn,max(c[i][0],dp[-1]-dp[i+1]))
print minn
```
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
"""
from bootcamp import Basebootcamp
import re
from bootcamp import Basebootcamp
class Cthedeliverydilemmabootcamp(Basebootcamp):
def __init__(self, max_n=5, a_max=10**9, b_max=10**9):
self.max_n = max_n
self.a_max = a_max
self.b_max = b_max
def case_generator(self):
import random
n = random.randint(1, self.max_n)
# 保证生成的a和b包含全配送/全自取等边界情况
a = [random.randint(1, self.a_max) for _ in range(n)]
b = [random.randint(1, self.b_max) for _ in range(n)]
# 强制添加一个b总和极小的案例
if random.random() < 0.2:
b = [1] * n
return {'n': n, 'a': a, 'b': b}
@staticmethod
def prompt_func(question_case) -> str:
prompt = (
"Petya需要订购n道不同的菜肴。每道菜可以选择由餐厅配送或自己取。配送的餐厅会同时派送总配送时间取其中的最大值。自己取的餐厅需要依次前往总时间是它们的累计值。最终总时间为配送时间的最大值与自己取的时间总和的较大者。\n\n"
"输入格式:\n"
"第一行是n表示菜肴数量。\n"
"第二行是n个整数a_i表示配送时间。\n"
"第三行是n个整数b_i表示自取时间。\n\n"
"当前测试用例:\n"
f"n = {question_case['n']}\n"
f"a = {question_case['a']}\n"
f"b = {question_case['b']}\n\n"
"请输出一个整数,表示所有可能方案中的最小总时间,并将其包裹在[answer]和[/answer]标签内,例如:[answer]5[/answer]。\n"
)
return prompt
@staticmethod
def extract_output(output):
# 提取最后一个answer标签内容
matches = re.findall(r'\[answer\](.*?)\[/answer\]', output, re.DOTALL)
return int(matches[-1].strip()) if matches else None
@classmethod
def _verify_correction(cls, solution, identity):
try:
return int(solution) == cls.compute_min_time(
identity['n'], identity['a'], identity['b']
)
except:
return False
@staticmethod
def compute_min_time(n, a, b):
sorted_pairs = sorted(zip(a, b), key=lambda x: x[0])
total_b = sum(b)
prefix_b = [0]
for a_i, b_i in sorted_pairs:
prefix_b.append(prefix_b[-1] + b_i)
min_time = total_b # 初始化为全自取的情况
for i in range(n):
current_a = sorted_pairs[i][0]
remaining_b = total_b - prefix_b[i+1]
min_time = min(min_time, max(current_a, remaining_b))
return min_time