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

269 lines
7.9 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.

"""#
### 谜题描述
Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read data in parallel.
When viewed from the side, Mike's hard drive is an endless array of tracks. The tracks of the array are numbered from left to right with integers, starting with 1. In the initial state the i-th reading head is above the track number hi. For each of the reading heads, the hard drive's firmware can move the head exactly one track to the right or to the left, or leave it on the current track. During the operation each head's movement does not affect the movement of the other heads: the heads can change their relative order; there can be multiple reading heads above any of the tracks. A track is considered read if at least one head has visited this track. In particular, all of the tracks numbered h1, h2, ..., hn have been read at the beginning of the operation.
<image>
Mike needs to read the data on m distinct tracks with numbers p1, p2, ..., pm. Determine the minimum time the hard drive firmware needs to move the heads and read all the given tracks. Note that an arbitrary number of other tracks can also be read.
Input
The first line of the input contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of disk heads and the number of tracks to read, accordingly. The second line contains n distinct integers hi in ascending order (1 ≤ hi ≤ 1010, hi < hi + 1) — the initial positions of the heads. The third line contains m distinct integers pi in ascending order (1 ≤ pi ≤ 1010, pi < pi + 1) - the numbers of tracks to read.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.
Output
Print a single number — the minimum time required, in seconds, to read all the needed tracks.
Examples
Input
3 4
2 5 6
1 3 6 8
Output
2
Input
3 3
1 2 3
1 2 3
Output
0
Input
1 2
165
142 200
Output
81
Note
The first test coincides with the figure. In this case the given tracks can be read in 2 seconds in the following way:
1. during the first second move the 1-st head to the left and let it stay there;
2. move the second head to the left twice;
3. move the third head to the right twice (note that the 6-th track has already been read at the beginning).
One cannot read the tracks in 1 second as the 3-rd head is at distance 2 from the 8-th track.
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
```python
#include <bits/stdc++.h>
using namespace std;
long n, m, pos;
long long h[100005], r[100005];
char s[1200000];
long long i, j, mij;
long long getLongLong() {
long long ans = 0;
while (s[pos] == ' ') pos++;
while ((s[pos] >= '0') && (s[pos] <= '9')) {
ans = ans * 10 + s[pos] - '0';
pos++;
}
return (ans);
}
long cb(long long x) {
long i = 1, j = m, mij;
do {
mij = (i + j) / 2;
if (x <= r[mij])
j = mij - 1;
else
i = mij + 1;
} while (i <= j);
return j;
}
int check(long long d) {
long pos = 1, i, did;
for (i = 1; i <= n; i++) {
if (h[i] <= r[pos]) {
while (r[pos] - h[i] <= d) pos++;
} else {
if (h[i] - r[pos] > d) return 0;
did = cb(h[i]);
while (r[did + 1] - r[pos] + min(h[i] - r[pos], r[did + 1] - h[i]) <= d)
did++;
pos = did + 1;
}
if (pos > m) return 1;
}
if (pos <= m) return 0;
return 1;
}
int main() {
scanf(\"%ld %ld\n\", &n, &m);
pos = 0;
gets(s);
for (i = 1; i <= n; i++) h[i] = getLongLong();
pos = 0;
gets(s);
for (i = 1; i <= m; i++) r[i] = getLongLong();
r[m + 1] = 1LL << 60;
i = 0;
j = 1e11;
do {
mij = (i + j) / 2;
if (check(mij))
j = mij - 1;
else
i = mij + 1;
} while (i <= j);
printf(\"%I64d\", i);
return 0;
}
```
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
"""
from bootcamp import Basebootcamp
import random
import re
from bootcamp import Basebootcamp
class Ereadtimebootcamp(Basebootcamp):
def __init__(self, max_heads=5, max_tracks=5, max_value=10**10):
self.max_heads = max_heads
self.max_tracks = max_tracks
self.max_value = max_value
def case_generator(self):
n = random.randint(1, self.max_heads)
m = random.randint(1, self.max_tracks)
# Generate heads with logarithmic distribution
h = sorted(random.sample(range(1, self.max_value+1), n))
if n > 1: # Ensure sorted and unique
h = sorted(list(set(h)))
while len(h) < n:
new_val = random.randint(h[-1]+1, self.max_value)
h.append(new_val)
# Generate targets with three types of coverage
p_candidates = set()
# Type 1: Existing head positions
p_candidates.update(h)
# Type 2: Boundary cases (min head ± delta, max head ± delta)
delta = self.max_value // 1000
p_candidates.add(max(1, h[0] - delta))
p_candidates.add(h[0] + delta)
p_candidates.add(max(1, h[-1] - delta))
p_candidates.add(h[-1] + delta)
# Type 3: Random distant points
for _ in range(max(m, 10)):
p_candidates.add(random.randint(1, self.max_value))
# Build sorted p list
p_list = sorted(p_candidates)
p = []
for num in p_list:
if not p or num > p[-1]:
p.append(num)
if len(p) == m:
break
# Fill remaining with distant values
while len(p) < m:
p.append(p[-1] + random.randint(1, self.max_value//100))
return {
'n': n,
'm': m,
'h': h[:n],
'p': sorted(p[:m])
}
@staticmethod
def prompt_func(question_case):
h = question_case['h']
p = question_case['p']
return f"""As a hard drive optimization engineer, determine the minimal time (in seconds) needed to read all required tracks.
Heads (sorted): {h}
Required tracks (sorted): {p}
Rules:
1. Each head can move left/right/stay each second
2. Any track visited by any head (including initial positions) is considered read
3. Find the minimal time where ALL required tracks are covered
Answer format: [answer]{{time}}[/answer]"""
@staticmethod
def extract_output(output):
matches = re.findall(r'\[answer\](.*?)\[/answer\]', output, re.DOTALL)
try:
return int(matches[-1].strip()) if matches else None
except (ValueError, TypeError):
return None
@classmethod
def _verify_correction(cls, solution, identity):
h = sorted(identity['h'])
p = sorted(identity['p'])
# Edge case: empty check
if not p or not h:
return solution == 0
# Binary search with precise coverage check
def is_feasible(d):
ptr = 0
for pos in h:
if ptr >= len(p):
return True
# Calculate coverage range
left = pos - d
right = pos + d
# Skip until first uncovered track
if p[ptr] > right:
continue
# Check impossible case
if p[ptr] < left:
return False
# Find maximal reachable track
max_reach = right
while ptr < len(p) and p[ptr] <= max_reach:
ptr += 1
return ptr >= len(p)
# Find minimal time
low, high = 0, max(abs(h[0]-p[-1]), abs(h[-1]-p[0]))
best = high
while low <= high:
mid = (low + high) // 2
if is_feasible(mid):
best = mid
high = mid - 1
else:
low = mid + 1
return solution == best