The beauty of programming 1.8 The third solution of Xiaofei's elevator scheduling algorithm

The original title is as follows:

The Sigma Building where Microsoft Research Asia is located has a total of 6 elevators. During peak hours, people go up and down on every floor, and the elevator stops on every floor. Intern Xiao Fei is often impatient with the elevators that stop on every floor, so he proposed such a solution:

Since the floors are not too high, we only allow the elevator to stop on one of the floors every time the elevator goes up from one floor during the busy commute time. All passengers get on the elevator from the first floor. After reaching a certain floor, the elevator stops, and all passengers climb the stairs from here to their destination floor. On the first floor, each passenger chooses his own destination floor, and the elevator calculates the floor where it should stop.
Question: Which floor does the elevator stop on so that all passengers who take the elevator this time can climb the least number of floors?

Solution 1: Calculate all from the first layer to the Nth layer, and record the minimum value and the stay layer. This is the first solution that most people think of, but those who pursue efficiency will definitely think of other ways, so there is a second solution

Solution 2: Assuming that the elevator stops on the i floor, we can first calculate the total number of floors Y that all passengers have to climb. If there are N1 passengers below the i floor, N2 passengers are on the i floor, and N3 passengers are above the i floor . At this time, if the elevator is changed to stop on the i-1 floor, all passengers whose destination is on the i floor and above will have to climb one more floor, a total of N2+N3 floors need to be climbed, and all passengers below i-1 and below can Climb one floor less, and climb N1 floor less in total, so the total number of floors passengers need to climb is Y-N1+N2+N3 = Y - (N1 - N2 - N3); otherwise, if they stop at i+1 floor, then The total number of floors to climb is Y + (N1 + N2 - N3). It can be seen that when N1 > N2 + N3, it is better to stop on the i-1 floor than to stop on the i floor, and when N1+N2<N3, it is better to stop on the i+1 floor than to stop on the i floor, otherwise, it is best to stop on the i floor. This algorithm can calculate the optimal solution in one cycle.

Solution 3: This is an algorithm given by a friend of mine. Assuming you stop at layer x and ni is the number of people on layer i, the total number of layers you need to climb is


Therefore, the x that makes the above formula obtain the minimum value is the solution of the problem, and the solution is as follows


In this way, we can also use SN - 2SK<0 to find the solution of the problem in one traversal


Explanation: I have thought about using the idea of ​​variance to solve this problem, so that the absolute value symbol can be eliminated, and the solution can be solved by deriving or expanding the formula into a symmetrical axis equation, but the solution obtained by this method is not necessarily is the correct solution, it has an error of plus or minus 1, it is not clear why





Guess you like

Origin blog.csdn.net/bira55/article/details/40583511