Elevator stopping Plan of greedy algorithm

Source: Internet
Author: User

Elevator stopping Plan

Description

ZSoft Corp. is Asoftware company in Gaoke Hall. And the workers in the hall is veryhard-working. But the elevator-in-hall always drives them crazy. Why? Because There is only one elevator in Gaoke Hall and while there was hundreds ofcompanies in it. Every morning, people must waste a lot of time waiting for Theelevator.
Hal, a smart guy in ZSoft, wants-to-change this situation. He wants to find away to make the elevator work more effectively. But it's not a easy job.
There is floors in Gaoke Hall. It takes 4 seconds for the elevator to raiseone floor. It means:
It costs (31-1) *4=120 seconds if the elevator goes from the 1st floor to the31st floor without stop. And the elevator stops second once. So, if theelevator stops at each floor, it'll cost 30*4+29*10 = 410 seconds (It's notnecessary to calculate the Stoppin G time at 31st floor). In another-on-the-ittakes, seconds for the workers-to-go or down-one floor. It takes 30*20 =600 seconds for them-walk from the 1st floor to the 31st floor. Obviously,it is isn't a good idea. So some people choose to use the elevator to get a Floorwhich are the nearest to their office.
After thinking through for a long time, the Hal finally found a to improve thissituation. He told the elevator man his idea:first, the elevator man asks ThePeople which floors they want to go. He'll then design a stopping plan whichminimize the time the last person need to arrive the floor where his officelocate S. For example, if the elevator are required to stop at the 4th, 5th and10th floor,the stopping plan would be:the elevator Stops at 4th and 10thfloor. Because the elevator would arrive 4th floor @ 3*4 = Second, then Itwill stop seconds and then it'll arrive 10th floor At 3*4+10+6*4 = second. People who want-to-go 4th floor would reach their office at second, Peoplewho want to go-5th floor would reach at 12+2 0 = Second and people who wantto go to 10th floor would reach at second. Therefore It takes seconds forthe last person to reach his office. It's a good deal for all people.
Now, you is supposed to write a program to help the elevator mans to design thestopping Plan,which minimize the time the L AST person needs to arrive at Hisfloor.

Input

The input consistsof several testcases. Each testcase are in a single line as the following:

N F1 f2 ... fn


It means, there is totally n floors at which the elevator need to stop, and n= 0 means no testcases any more. F1 F2 ... fn is the floors at which Theelevator was to be stopped (n <=, 2 <= F1 < F2 ... fn <= 31). Every number is separated by a single space.

Output

For each testcase,output the time of the last reading person needs in the first line and thestopping floors in the second Lin E. Please note this there is a summary of thefloors on the head of the second line. There may several solutions, anyappropriate one is accepted. No extra spaces is allowed.

Sample Input

3 4 5 10

1 2

0

Sample Output

46

2 4 10

4

1 2

The title means: Elevator on the first floor needs 4s, in a floor to stay 10s, people on a floor need 20s. Now we need to design an algorithm that will make it the shortest time for people to arrive at the office.

Problem Solving Ideas:

The idea of this problem is greedy algorithm + least squares method .

In the subject, the shortest time is 0, the longest time to stay on each floor of the time . To speed up the search, the dichotomy is used here.

Assuming that the current time is T, it is not considered ( up to a maximum of t/20+1) to climb stairs within the T-time. Need to start thinking (t/20+2). Starting from the T/20+2 layer to search for passengers need to stop the floor, if there are passengers need to stop on the level I, then the elevator should be the best to stop in order to make the passengers who want to go to the I layer in the T time to reach layer I? Naturally, the higher the floor is, the better, assuming that the floor of the dock is J, then there is a critical equation 10*cnt + (j-1) * 4 + (j-i) *20 =t, where CNT for the number of times that have been stopped before, the solution j= (t-10*cnt+20*i+4)/24 .

When the elevator is parked on the J floor, some passengers can walk down the elevator to the floor above J, and will arrive in T time, this part of the floor does not have to be considered, the highest floor above J is I, then there is a critical equation (i-j) *20+10*n+ (j-1) = t , i= (t-10*cnt+16*j+4)/20, the next next from i= (T-10*CNT+16*J+4)/20+1 to consider passengers need to stop the floor.

According to the above thought, the procedure is as follows:

#include <iostream>

Using NAMESPACESTD;

#define MAXFLOOR35//maximum number of floors

#defineelevatorTime 4//The time required for the elevator to rise one floor

#define STOPTIME10//elevator stay in the first floor of the time

#define PEOPLETIME20//person to walk on the first floor of the time required

Intfloors[maxfloor]; Storage needs to stop the floor, floors[i]=1 that the layer I need to stay

Intstop[maxfloor]; Store the floor where the elevator finally stays

int n; Total number of floors that need to be stopped

int Topfloor; The highest floor of a parked floor

int mid; Time variable, which ultimately is the shortest time to output time

int cnt; Number of actual stops of the elevator

void result (); Finding the result function

BOOL Judge (INTTMP); Judging the TMP time is not enough

int solve (); Final Solution

int main ()

{

cin>>n;

while (n!=0)

{

Result ();

cin>>n;

}

return 0;

}

void result ()

{

int t;

for (int i=0;i<maxfloor;i++)

{

Clear Floor

floors[i]=0;

}

for (int i=0;i<n;i++)

{

Since the input is in ascending order, the last floor entered is the highest

cin>>t;

Floors[t]=1;

topfloor=t;

}

Judge (Solve ());

cout<<mid<<endl;

cout<<cnt;

for (int i = 0; i < cnt; i++)

cout<< "" <<stop[i];

cout<<endl;

}

BOOL Judge (INTTMP)

{

CNT = 0;

memset (stop, 0, sizeof (stop));

Since the floor is calculated from 1, the intermediate interval is tmp/peopletime, so find it starting from Tmp/peopletime + 2

for (int i = tmp/peopletime + 2; I <=topfloor; i++)

{

The while loop is used to find the floor that needs to stop

while (!floors[i] && i< topfloor)

i++;

T indicates the time required to run to I

int t = (i-1) * elevatortime +cnt * stoptime;

if (TMP < T)

return false;

Calculate by formula

t = (Tmp-stoptime * CNT +peopletime * i + elevatortime)/(Peopletime+elevatortime);

i = (Tmp-stoptime * cnt + (peopletime-elevatortime) * t + elevatortime)/peopletime;

stop[cnt++] = t;

}

return true;

}

int Solve ()

{

L shortest time, R maximum time

int L = 0, R = (stoptime+elevatortime) * (TOPFLOOR-1);

while (L < R)

{

Mid = (L + R + 1)/2;

if (mid = = R)

Break

if (judge (mid))

R = mid;

Else

L = mid;

}

return mid;

}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Elevator stopping Plan of greedy 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.