Elevator scheduling algorithm (Microsoft interview questions)

Source: Internet
Author: User

During peak hours, intern Xiaofei was often impatient to stop on every floor of the elevator, so he came up with a way: Because the floor is not high, so during busy hours, every time the elevator goes up from the first floor, we only allow the elevator to stop on one of them. All passengers are taken from the elevator upstairs. When they arrive at a certain floor, the elevator will hear that all passengers will climb the stairs from here to their target floor. 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? Can we ensure that the sum of the floors of all passengers who take the elevator to climb the stairs is the least?

 

Solution 1: Use the enumeration method O (N ^ 2)

# Include <vector> <br/> # include <iostream> <br/> using namespace STD; </P> <p> int main (void) <br/>{< br/> int I, j, N, person, temp, ANS, layer; <br/> vector <int> num; <br/> while (CIN> N) // indicates N layers <br/> {<br/> for (I = 1; I <= N; I ++) <br/>{< br/> CIN> person; // number of people in the elevator on the nth floor <br/> num. push_back (person); <br/>}< br/> ans =-1; <br/> for (I = 1; I <= N; I ++) <br/> {<br/> temp = 0; <br/> for (j = 1; j <= N; j ++) <br/> {<br/> if (I> = J) <br/> temp + = num [J-1] * (I-j ); <br/> else <br/> temp + = num [J-1] * (J-I ); <br/>}< br/> If (ANS =-1 | temp <ans) <br/> {<br/> ans = temp; <br/> layer = I; <br/>}< br/> cout <"minimum layers:" <ans <Endl; <br/> cout <"under" <layer <"layer" <Endl; <br/>}< br/> return 0; <br/>}

 

Solution 2:

Linear Algorithm O (N)
1: calculate the total number of floors where all passengers climb the elevator at the first layer through a scan.
2: The final result is obtained through dynamic changes at each layer.
For example, the total number of floors where all passengers climb the elevator on layer I is temp.
Under the I layer, num1 is in the I layer, num2 is in the I layer, and num3 is in the I layer.
If the temp + I-1 under the num3-num1-num2 Layer
If the temp + num1 + num2-num3 under layer I + 1
# Include <vector> <br/> # include <iostream> <br/> using namespace STD; </P> <p> int main (void) <br/>{< br/> int num1, num2, num3, I, n, ANS, person, layer; <br/> vector <int> num; <br/> while (CIN> N) <br/> {<br/> vector <int> sum (n + 2 ); <br/> sum [0] = 0; <br/> ans = 0; <br/> num. push_back (0); <br/> for (I = 1; I <= N; I ++) <br/>{< br/> CIN> person; // Number of elevator persons under the nth floor <br/> num. push_back (person); <br/> sum [I] = sum [I-1] + num [I]; <br/> ans + = num [I] * (I-1 ); <br/>}< br/> // cout <"Ans =" <ans <Endl; <br/> layer = 1; <br/> for (I = 2; I <= N; I ++) <br/> {<br/> num2 = num [I-1]; <br/> num1 = sum [I-1]-num2; <br/> num3 = sum [N]-sum [I-1]; <br/> If (num1 + num2 <num3) <br/> {<br/> ans = ans + num1 + num2-num3; <br/> layer = I; <br/>}< br/> cout <"minimum number of layers:" <ans <Endl; <br/> cout <"under" <layer <"layer" <Endl; <br/>}< br/>}

 

Solution 2 improvements:

# Include <vector> <br/> # include <iostream> <br/> using namespace STD; </P> <p> int main (void) <br/>{< br/> int num1, num2, num3, I, n, ANS, person, layer; <br/> vector <int> num; <br/> while (CIN> N) <br/> {<br/> ans = 0; <br/> num. push_back (0); <br/> num3 = 0; <br/> for (I = 1; I <= N; I ++) <br/>{< br/> CIN> person; // number of people in the elevator on the nth floor <br/> num. push_back (person); <br/> ans + = num [I] * (I-1); <br/> num3 + = num [I]; <br/>}< br/> // initialization <br/> num2 = num [1]; <br/> num3-= num2; <br/> num1 = 0; <br/> layer = 1; </P> <p> for (I = 2; I <= N; I ++) <br/> {<br/> If (num1 + num2 <num3) <br/> {<br/> ans = ans + num1 + num2-num3; <br/> layer = I; <br/> num1 + = num2; <br/> num2 = num [I]; <br/> num3-= num [I]; <br/> // dynamic change, programming skills <br/>}< br/> else <br/> break; <br/> // use a property, the optimal number of layers must be in the middle, and there are more <br/>}< br/> cout <"minimum number of layers: "<ans <Endl; <br/> cout <" under "<layer <" layer "<Endl; <br/>}< br/>}

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.