1.8 Xiaofei's elevator Scheduling Algorithm

Source: Internet
Author: User

Question: There is a building, and now we have designed an elevator scheduling algorithm: the elevator is on the first floor to let everyone go to the elevator, and then calculate the I of a Certain Floor Based on the floor selected by everyone, the elevator stops at layer I to let everyone go down the elevator, and then everyone climbs the stairs to reach their own floor. Which floor does the elevator stop at? Can the sum of floors climbed by all passengers this time be shortest?

(1)

The most direct and simplest method is to enumerate the first and last floors, and then calculate which floor the elevator stops at, which will minimize the total floor crawling for all passengers.

The Code is as follows:

Int nperson []; // nperson [I] indicates the number of passengers on layer I. Int nfloor = 0, nminfloor = 0; int ntargetfloor =-1; for (INT I = 1; I <= N; ++ I) {// n represents the total number of floors for (Int J = 1; j <I; ++ J) nfloor + = nperson [J] * (I-j); For (Int J = I + 1; j <= N; ++ J) nfloor + = nperson [J] * (J-I); If (ntargetfloor =-1 | nfloor <nminfloor) {nminfloor = nfloor; ntargetfloor = I ;}}

(2)

Train of Thought: when the elevator stops at layer I, the total number of stairs a passenger wants to climb is Y. assume that there are N1 passengers to reach the layers <I, N2 passengers to reach the layers = I, and N3 passengers to reach the layers> I. so there are: (1) when the elevator to stop at the I-1, then Y + (n2 + N3-N1) (2) when the elevator to stop at I + 1, then Y + (N1 + N2-N3) therefore, when the value of the following part is <0 (such as N1 + N2 <N3 in (2), the total number of stairs after a negative number is smaller than the original one, which is a better solution. therefore, we can use the above policies to evaluate the values of N1, N2, and N3 from the first layer and adjust them in order to obtain the optimal solution.

Int nperson []; // nperson [I] indicates the number of passengers on layer I. Int nfloor = 0, nminfloor = 0; int ntargetfloor = 1; int n1 = 0, n2 = 0, N3 = 0; For (n1 = 0, n2 = nperson [1], N3 = 0, I = 2; I <= N; ++ I) {// n indicates the number of floors N3 + = nperson [I]; nminfloor + = nperson [I] * (I-1) ;}for (INT I = 2; I <= N; ++ I) {If (N1 + N2 <N3) {ntargetfloor = I; nminfloor + = (N1 + N2-N3); N1 + = n2; n2 = nperson [I]; N3-= nperson [I];} else break ;}





1.8 Xiaofei's elevator Scheduling Algorithm

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.