Vào thời trung cổ, các quân sĩ đã sử dụng một cách rất độc đáo để loan tin trên chiến truờng. Khi nhận đuợc một tin cấp báo, tất cả các lính gác đều biết có tin cấp báo nhưng không biết nội dung tin. Nguời lính canh đầu tiên nhận đuợc nội dung tin sẽ vừa chạy, vưa hô vang nội dung thông tin mình nhận đuợc. Trong lúc đó, những lính gác lân cận cũng sẽ chạy để nhận thông tin. Các lính gác có thể chạy theo các huớng, với tốc độ tối đa 1m/s. Một lính canh chỉ có thể nghe đuợc nội dung thông báo từ nguời biết tin nếu anh ta cách nguời đó không quá K(m). Khi nguời lính thứ 2 nhận đuợc thông tin anh ta cũng tiếp tục chạy và hô vang thông tin để truyền tin. Thao tác trên đuợc lập lại cho đến khi tất cả các lính canh nhận đuợc tin.
Yêu cầu: Cho biết vị trí ban đầu của các lính canh. Hãy viết một chương trình cho biết thời gian tối thiểu để tất các lính canh nhận đuợc thông tin.
Dữ liệu: Vào từ file văn bản BAOTIN.INP, dòng đầu cho biết K (0 ≤ K ≤ 106), dòng thứ 2 cho biết số luợng lính canh N (1≤ N ≤ 100000). N Dòng tiếp theo, mỗi dòng là một số thực, cho biết khỏang cách của N lính canh so với lính canh đầu tiên. Nhiều lính canh có thể có vị trí trùng nhau.
Kết quả: Ghi ra file văn bản BAOTIN.OUT là số thực t (lấy 3 số lẻ) cho biết thời gian tối thiểu truyền tin cho tất cả các lính canh sau khi nguời lính đầu tiên nhận tin.
Ví dụ:
BAOTIN.INP BAOTIN.OUT
3.000
2
0.000
6.000 1.500
2.000
4
0.000
4.000
4.000
8.000 1.000
use binary search
Assistant
To solve the problem of finding the minimal time ( t ) required for all guards to receive the message, we can utilize a binary search approach. Below is a step-by-step explanation of the solution:
Problem Analysis
Sorting Positions:
The guards are placed linearly, so it's beneficial to sort their positions in ascending order. This allows us to efficiently evaluate the coverage from left to right.
Binary Search on Time:
We perform a binary search on the time ( t ) to find the minimal value that satisfies the coverage condition.
Lower Bound: ( 0 )
Upper Bound: The maximum possible distance divided by the speed, which can be initially set based on the positions and the communication range ( K ).
Coverage Condition:
For a given ( t ), the first guard can cover the range ([x_1 - t - K, x_1 + t + K]).
For each subsequent guard, check if their position ( x_i ) is within the current coverage range. If not, the current ( t ) is insufficient.
Update the coverage range by extending it with the current guard's new coverage based on ( t ) and ( K ).
Termination:
The binary search continues until the lower and upper bounds converge within a reasonable precision (e.g., ( 10^{-4} )).
Edge Cases:
If all guards are within a single coverage range initially, the minimal ( t ) could be zero.
Implementation
The following Python code implements the described approach:
python
import sys
import sys
import math
def read_input():
import sys
data = sys.stdin.read().split()
idx=0
K = float(data[idx]); idx+=1
N = int(data[idx]); idx+=1
positions = []
for _ in range(N):
positions.append(float(data[idx])); idx+=1
return K, N, positions
def is_possible(t, K, positions, N):
last_cover = positions[0] + t + K
for i in range(1, N):
if positions[i] - t > last_cover:
return False
last_cover = max(last_cover, positions[i] + t + K)
return True
def find_min_time(K, N, positions):
positions.sort()
low = 0.0
high = positions[-1] - positions[0] + K
for _ in range(100): # sufficient for precision
mid = (low + high)/2
if is_possible(mid, K, positions, N):
high = mid
else:
low = mid
return high
def main():
K, N, positions = read_input()
min_t = find_min_time(K, N, positions)
print("{0:.3f}".format(min_t))
if __name__ == "__main__":
main()
Explanation of the Code
Input Reading:
The read_input function reads the input values for ( K ), ( N ), and the positions of the guards.
Feasibility Check:
The is_possible function determines if all guards can receive the message within a given time ( t ). It does this by iterating through the sorted positions and ensuring each guard is within the coverage range.
Binary Search:
The find_min_time function performs the binary search to find the minimal ( t ) that satisfies the coverage condition. It iterates sufficiently (e.g., 100 times) to achieve the required precision.
Output:
The minimal time ( t ) is printed with three decimal places as per the problem's requirement.
Conclusion
This solution efficiently finds the minimal time ( t ) using a binary search approach, ensuring that all guards receive the message within the shortest possible time while adhering to the movement and communication constraints.
Answer
python
import sys
import sys
import math
def read_input():
import sys
data = sys.stdin.read().split()
idx=0
K = float(data[idx]); idx+=1
N = int(data[idx]); idx+=1
positions = []
for _ in range(N):
positions.append(float(data[idx])); idx+=1
return K, N, positions
def is_possible(t, K, positions, N):
last_cover = positions[0] + t + K
for i in range(1, N):
if positions[i] - t > last_cover:
return False
last_cover = max(last_cover, positions[i] + t + K)
return True
def find_min_time(K, N, positions):
positions.sort()
low = 0.0
high = positions[-1] - positions[0] + K
for _ in range(100): # sufficient for precision
mid = (low + high)/2
if is_possible(mid, K, positions, N):
high = mid
else:
low = mid
return high
def main():
K, N, positions = read_input()
min_t = find_min_time(K, N, positions)
print("{0:.3f}".format(min_t))
if __name__ == "__main__":
main()