Beauty of programming-another solution to Xiaofei's elevator Scheduling Algorithm

Source: Internet
Author: User

After the summer vacation, I was idle at home. I had some gains by turning over the book "The beauty of programming" I bought last year. Yesterday, I saw Xiao Fei's elevator scheduling algorithm. I thought about it and got a solution different from the standard answer provided in the book.

 

I. Problem description:

There are 6 elevators in the magma building where Microsoft Research Asia is located. During peak hours, there are people on and off each layer, and the elevator stops on each layer. Intern Xiaofei is often impatient with the elevator on every floor, so he proposed the following method:
Because the floor is not too high, so in the busy working hours, every time the elevator goes up from the first floor, we only allow the elevator to stop on one of them. All passengers go from the elevator upstairs. When they arrive at a certain layer, the elevator stops and all passengers climb the stairs from here to their target layer. On the first floor, each passenger chooses the target layer. On the first floor, each passenger chooses his or her target layer, and the elevator automatically calculates the floor to be stopped.

Q: Which floor does the elevator stop on? This ensures that the sum of the floors of all passengers who take the elevator to climb the stairs is the least.

 

Ii. Solutions in books

The book provides two solutions:

The abstract model selected in the book before a specific solution is provided. The model is as follows:

Assume that there are n floors in total, the elevator stops at Layer X, and the number of passengers to go to layer I is tot [I]. In this way, the total number of floors to be crawled is Σ I {tot [I] * | I-x |}

Solution 1: brute force.

This is the easiest solution to think. Start from the second floor to enumerate X to the nth floor, and then calculate the total number of floors for all passengers to climb if the elevator stops on the second floor. The time complexity of this solution is O (n2 ).

Solution 2:

Assuming that the elevator stops at the I floor, we can calculate that the number of floors for all passengers to climb is Y. Suppose there are N1 passengers under the I floor and N2 passengers under the I floor, when the N3 passengers are above the I-floor, when the elevator stops at the I + 1 layer, the N1 + N2 passengers need to go to the next floor, with a total of N1 + N2 passengers, n3 passengers need to climb a floor above, less N3 floor, at this time y (I + 1) = y (I) + N1 + N2-N3, obviously, when N1 + N2 <N3, y decreases continuously. Y1 is easy to calculate. In addition, we can also see that N1 + N2 is increasing and N3 is decreasing. Therefore, once N1 + N2 is greater than N3, we can directly exit the loop, there is no need to calculate it. The time complexity of this solution is O (n ).

 

3. My Solutions

The above two solutions are provided directly in the book. Because I did not directly answer the question after reading it, the abstract model obtained is inconsistent with the model mentioned above, And I embarked on another path.

The following is my thinking process.

Assume that the number of passengers is N, and the corresponding floor is N1, N2,..., NN. Requires n so that min {Σ I | n-Ni |}

If we can sort the sequence N1, N2,..., NN, we can obtain an ordered sequence NK1, nk2,..., nkn. The problem can be expressed as: evaluate N, so that min {Σ I | n-NKI |} (NK1 <= nk2 <=... <= nkn ).

Think of the formula above as a function about N, and we can easily imagine the curve of this function, which is probably the curve of a two-dimensional function with a port facing up.

The following question is how to find the minimum value of n. In fact, it is very direct to imagine the function curve.

If n is an odd number, the minimum value can be obtained when n is the median.

If n is an even number, N obtains the two numbers in the middle of an ordered sequence or any number between the two numbers to obtain the minimum value. For example, if the sequence is, then n = or 5 is the minimum value.

The above two conclusions prove very simple. I will not go into details here. If you are interested, you may wish to draw a picture on the paper.

 

As for how to find a median without sequence, there are many methods and different requirements for time space. This issue may be discussed in the next blog.

When faced with a specific problem, the first step is to find a suitable abstract model to describe the problem. Different abstract models may produce different solutions, even some abstract models can solve the problem, while others cannot. Even if they can solve the problem, the efficiency of solving the problem may be greatly different. In this example, a different abstract model is selected and a different solution is obtained.

Abstract models are important.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.