The 10th Blue Bridge Cup java-c group-sweeping robot

1. Problem description:

The office area of ​​Xiaoming Company has a long corridor composed of N square areas, as shown in the figure below. K sweeping robots are deployed in the corridor, of which the i-th is in the Ai-th grid area. It is known that the sweeping robot can move to the adjacent squares on the left and right every minute and clean the area. Please write a program to calculate the cleaning route of each robot, so that they all return to the starting square, and each square area is cleaned at least once. It takes the least time from the start of the robot's action to the return of the last robot. Note that multiple robots can clean the same square area at the same time, and they will not affect each other.
The least time it takes to output. In the example shown in the figure above, the minimum time spent is 6. The first route: 2-1-2-3-4-3-2, cleared the area 1, 2, 3, and 4. The second route was 5-6-7-6-5, 5, 6, and 7 were cleaned. The third route 10-9-8-9-10, cleaned 8, 9 and 10.
[Input format] The
first line contains two integers N and K. Next K lines, each line has an integer Ai.
[Output format]
Output an integer to represent the answer.
[Sample input] 10 3 5 2 10
[Sample output] 6
[Evaluation use case scale and conventions]
For 30% of evaluation use cases, 1≤ K <N ≤10. For 60% of the evaluation use cases, 1≤ K <N ≤1000. For all evaluation use cases, 1≤ K <N ≤100000 and 1≤ Ai ≤ N.

2. Thinking analysis:

① I didn't have any ideas at the beginning, so I looked for a blog on the Internet and found that it can be solved after understanding. I found a better understandable code in the c language network and converted it to python. The answer to this question actually depends on the time taken for the last robot to return to its position after completing the sweeping task. Therefore, K robots must cooperate with each other. Only when the area cleaned by K robots is averaged, the time used at this time is the shortest. , Because this time is working at the same time. Analyzing the problem, we can know that the length of the minimum area that each robot can clean is max(robots[0],n // k), and the maximum length is n, so we find a value in this range so that the area that the robot can clean can meet The requirements of the question (in fact, you can use binary search to solve the problem and it will take less time)

 ② Our strategy is to update the left boundary of the next robot cleaning according to the length of the cleaning interval currently passed into the method, according to the position of each robot, the length of the cleaning interval, and the position of the left boundary l (each robot completes its own cleaning Task), so at the beginning, you need to declare a left boundary variable to record the left boundary of the next robot to start cleaning. When updating the left boundary, you can directly return False when the condition of the problem is not met. When the loop ends Check the maximum range that the robot can clean to see if it is greater than or equal to N, if it is satisfied, it can return True, otherwise it returns False

③ When judging whether the length of the current cleaning interval passed meets the conditions of the question, the core is the following code:

When the robot appears on the left side of the boundary (L: boundary, i: the subscript of the current robot, Q: interval size)
L = i + Q -1
when the robot appears on the right side of the boundary
L = i + Q-(i-L)

④ You can test the code on the c language network

3. The code is as follows:

from typing import List


def f(l: int, Q: int, robots: List[int]):
    for i in range(k):
        if robots[i] - Q <= l:
            if robots[i] <= l:
                l = robots[i] + Q - 1
            else:
                l = robots[i] + Q - (robots[i] - l)
        else:
            return False
    if l < n: return False
    return True


if __name__ == '__main__':
    n, k = map(int, input().split())
    robots = list()
    for i in range(k):
        robots.append(int(input()))
    robots.sort()
    Q = n // k if n // k >= robots[0] else robots[0]
    while Q <= n:
        l = 0
        if f(l, Q, robots): break
        Q += 1
    print(2 * (Q - 1))

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/115035429