Beautiful programming Reading Notes 1.8-Xiaofei's elevator Scheduling Algorithm

Source: Internet
Author: User

Http://blog.csdn.net/pipisorry/article/details/36688019

Problem:

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 his or her target layer, and the elevator 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 3:

When there are only two persons, one goes to the K1 layer and the other goes to the K2 layer. No matter the elevator stops on any floor between the K1 and K2 layers, the total cost of the two persons is | K2-K1 |.

Expansion of N people, the elevator will not change as long as it stops at the median, otherwise it will increase. Therefore, you only need to find the median. If n is an even number, either of the two numbers in the middle is OK.

int nPerson[N+1];  int target_floor;    int left = 1,right = N;  while(right-left > 1)  {      while(nPerson[left] == 0)left++;      nPerson[left] --;      while(nPerson[right--] == 0)right--;      nPerson[right] --;  }  return left;  

/*************************************** *************************************//* Beauty of programming-Xiaofei's elevator scheduling algorithm Pipi 2014-7-2 *//************************* **************************************** * **********/# include <stdio. h> # include <malloc. h> int partition (int * a, int P, int R) {// contains P, RIF (P = r) return P; int partition = A [R]; int low = p-1; int TMP; For (INT high = P; high <= R-1; high ++) {if (a [High] <strong) {LOW ++; if (low! = High) {TMP = A [low]; A [low] = A [High]; A [High] = TMP ;}}} A [R] = A [Low + 1]; A [Low + 1] = lower; return low + 1;} int randomizedselect (int * a, int P, int R, int POS) {// p pos all start from 0 if (P = r) return a [p]; int posfind = partition (A, P, R ); if (Pos = posfind) return a [posfind]; else if (Pos <posfind) return randomizedselect (A, P, posfind-1, POS); elsereturn randomizedselect (, posfind + 1, R, POS);} void elevator () {int N, I, medium; int * A; scanf ("% d", & N ); A = (int *) malloc (N * sizeof (INT); for (I = 0; I <n; I ++) scanf ("% d ", & A [I]); medium = randomizedselect (A, 0, n-1, n/2); printf ("% d", medium);} int main () {ELEVATOR (); Return 0 ;}


Solution to expansion problems:
1. If you are tired of going up the stairs, it is easier to go down. If you go up one layer, it consumes K units of energy. If you go down one layer, it only consumes 1 unit of energy.

Http://blog.csdn.net/li4951/article/details/7486092

2 elevator can be stopped K times solution:

Http://blog.163.com/guixl_001/blog/static/41764104201082062317857/


Ref:

Http://blog.csdn.net/pipisorry/article/details/36688019

Http://blog.csdn.net/lonelycatcher/article/details/7910877

Http://blog.csdn.net/flyinghearts/article/details/5605931


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.