The beauty of programming 1.8 elevator scheduling algorithm for small flying

Source: Internet
Author: User

Topic:

There are 6 elevators in the Sigma Building, where Microsoft Research is located. At rush hour, everyone on each floor is up and down, and the elevator stops every floor. Interns are often very impatient with the elevator that stops at each floor, so he puts forward such a way:
As the floors are not too high, we only allow elevators to stop at one of the floors when the elevator goes up from the first floor during busy commuting hours. All passengers from an upstairs elevator, to a certain level, the elevator stopped, all the passengers from here to climb the stairs to their own destination layer. On the first floor, each passenger chooses his own destination, and the elevator calculates the floor that should be stopped.

Q: Which floor is the elevator parked on, which ensures that all passengers who take the elevator have the least number of floors to climb the stairs?

method One: brute force enumeration, Time complexity O (n^2)

Use array Nperson[i] to store the number of people downstairs on level I, stop at different I levels, ask everyone to walk the sum of floors, find the sum of the smallest floor target, output. Complete the calculation with a double loop.

1 /*2 * 1.8.13 *4 * Created On:jan 9,5 * Author:seekhit6 */7#include"iostream"8 #defineN 1009 Ten using namespacestd; One  A intFuncintNintnperson[]) { -  -     intNfloor, nminfloor=0, targetfloor=0; the  -      for(inti =1; I <= N; i++){ -Nfloor =0; -          for(intj =1; j<i; J + +) +Nfloor + = (i-j) *Nperson[j]; -          for(intj = i +1; J <= N; J + +) +Nfloor + = (j-i) *Nperson[j]; A  at         if(targetfloor==0|| Nminfloor>nfloor) {//If no targetfloor==0,nminfloor>nfloor is always met, do not execute if content -Nminfloor =Nfloor; -Targetfloor =i; -  -         } -     } in     returnTargetfloor; - } to  + intMain () { -     intN, Target; the     intNperson[n]; *cout<<"Enter the total number of floors N:"<<Endl; $Cin>>N;Panax Notoginsengcout<<"Enter the number of people downstairs on each floor"<<Endl; -      for(inti =1; I <= N; i++){ thecout<<"Section"<<i<<"number of people under the layer:"<<Endl; +Cin>>Nperson[i]; A     } the  +Target =func (n, Nperson); -cout<<"the optimal number of floors for elevator docking is:"<<Target<<Endl; $}

method Two: a dynamic programming algorithm with TIME complexity O (N)

Reduce the complexity of time, solve. If you stop at layer I, you can calculate the total number of floors Y that all passengers need to crawl. Set N1 individuals below the I floor downstairs, N2 individuals on the I floor downstairs, N3 individuals above the I floor downstairs.

If the elevator now to i-1 layer docking, now all I layer and I layer above the first floor, the equivalent of a total of more than climbed the N2+N3 layer , and I below the level of everyone less climbed the first floor, is less N1 layer. The total number becomes the n2+n3-n1 layer , if the n2+n3-n1<y, indicating a lower level of docking more excellent.

Conversely, if the i+1 layer is docked, the number of floors that all passengers climb becomes n1+n2-n3, if the n1+n2-n3<y indicates a higher docking level.

The process of the book is to use the way to judge the building, you can also use down the building to dynamically plan the best number of floors.

1 /*2 * 1.8.2 Dynamic planning3 *4 * Created On:jan 9,5 * Author:seekhit6 */7#include"iostream"8 #defineN 1009 Ten using namespacestd; One  A intFuncintNintnperson[]) { -  -     intNminfloor=0, targetfloor=1; the     intn1=0, n2=nperson[1],n3=0; -  - //calculate N3, the total number of floors that need to go when docked on the 1 floor -      for(inti =2; I <= N; i++){ +n3+=Nperson[i]; -nminfloor+=nperson[i]* (I-1); +     } A      at      for(intI=2; i<=n;i++){ -         if(n1+n2<N3) { -Targetfloor=i; -nminfloor+= (n1+n2-N3); -n1+=N2; -n2+=Nperson[i]; inn3-=Nperson[i]; -         } to         Else +              Break; -     } the     returnTargetfloor; * } $ Panax Notoginseng intMain () { -     intN, Target; the     intNperson[n]; +cout<<"Enter the total number of floors N:"<<Endl; ACin>>N; thecout<<"Enter the number of people downstairs on each floor"<<Endl; +      for(inti =1; I <= N; i++){ -cout<<"Section"<<i<<"number of people under the layer:"<<Endl; $Cin>>Nperson[i]; $     } -  -Target =func (n, Nperson); thecout<<"the optimal number of floors for elevator docking is:"<<Target<<Endl; -}

Operation Result:

Extension issues:

Climb up than go down to be tired, climb up a layer consumes the energy of K units, go down only consumes 1 units of energy, ask everyone to consume the least energy.

Solution:

Just turn the calculation n1+n2-n3 into N1+n2-n3*k. The rest are the same.

The beauty of programming 1.8 elevator scheduling algorithm for small flying

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.