The beauty of programming reading notes 1.8-small fly elevator scheduling algorithm

Source: Internet
Author: User

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

Problem:

The Sigma Building, where Microsoft Research is located in Asia, has 6 elevators together.

At rush hour, everyone on each floor is up and down. The elevator stops on every floor. Interns are often very impatient with elevators that stop at every level. So he put forward such an approach:

Because the floor is not too high, then in the busy commuting time, every time the elevator from the first floor, we just agreed that the elevator stopped in one of the layers. All passengers from an upstairs elevator. After arriving at a certain level, the elevator stops. All the passengers then climbed the stairs from here to their own destination layer. On the first floor, each passenger chooses his or her destination, and the elevator calculates the floor that should be stopped.


Q: Which floor is the elevator parked in, can guarantee that all the passengers on the elevator climb stairs to the minimum number of floors?


Solution 3:

There are only two people, one to K1 layer. One to the K2 floor, no matter what floor the elevator stops in the middle of the K1 to the K2 floor, the total cost of two people is | K2-k1 |.

Expand n Individuals. Elevators only to stop at the median cost will not change, otherwise it will be added. So just demand out the median can be, assuming that n is even, stop in the middle of the two number no matter what one 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;  

/****************************************************************************//* Programming Beauty 1.8-small fly elevator scheduling algorithm leather 2014-7-2*// /#include <stdio.h> #include <malloc.h>int partition (int* a,int p,int R) {//Includes p,rif (p = = r) Return P;int pivot = a[r];int Low = P-1;int tmp;for (in T high = p;high<=r-1;high++) {if (A[high]<pivot) {low++;if (Low! = high) {TMP = A[low]; A[low] = A[high]; A[high] = tmp;}}} A[r] = A[low + 1]; A[low + 1] = Pivot;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 (A,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++) scan F ("%d", &a[i]); medium = Randomizedselect (a,0, n-1, N/2);p rintf ("%d", medium);} InchT Main () {elevator (); return 0;} 


Solutions for scaling problems:
1 If you climb the stairs more tired, go down easier, if you go up a layer of energy that consumes K units, go down a layer of only 1 units of energy.


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

2 Elevators can stop 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


The beauty of programming reading notes 1.8-small fly 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.