SRM 156 Division-I, Level 2

Problem Statement
   
You have been put in charge of writing the sofware that powers an elevator in a very tall office building. Because there is only one elevator in the building, and many floors, the designers of the elevator gave it the capacity to remember past usage history. By assuming that the employees will probably use the elevator in the same ways and at the same times each day (arriving at work, going to the cafeteria, leaving for the day), the software can be a much better judge of where to travel and when. For example, if it's currently on the fifth floor, and it knows that the next employee to use the elevator will be on the third floor, it can travel there early so that the employee will not have to spend any time waiting.
Given the arrival times, starting floors, and destination floors of each employee for the day, determine the fastest time in which the elevator can get each employee to his or her destination. The elevator starts on floor 1 at time 0. Travelling from floor p to floor q takes (abs(p - q)) units of time. Also, the elevator may stand still for any length of time, if that is optimal. Assume that it takes no time for employees to enter or exit the elevator. Your method should return the minimal value of T such that, at time T, all employees have arrived at their destinations. Employee i corresponds to the ith element of each input parameter.
Definition
   
Class:
SmartElevator
Method:
timeWaiting
Parameters:
int[], int[], int[]
Returns:
int
Method signature:
int timeWaiting(int[] arrivalTime, int[] startingFloor, int[] destinationFloor)
(be sure your method is public)
   

Constraints
-
arrivalTime will contain between 1 and 5 elements, inclusive.
-
arrivalTime, startingFloor, and destinationFloor will all contain the same number of elements.
-
Each element in arrivalTime will be between 1 and 1000000, inclusive.
-
Each element in startingFloor will be between 1 and 1000000, inclusive.
-
Each element in destinationFloor will be between 1 and 1000000, inclusive.
-
For all valid i, startingFloor[i] will never be equal to destinationFloor[i].
Examples
0)

   
{5}
{30}
{50}
Returns: 49
In this example, there is only one passenger. He will arrive on floor 30 at time 5, but the elevator will not be able to get to floor 30 until time 29. It will then pick up the employee and drop him off at floor 50 at time 49.
1)

   
{100}
{30}
{50}
Returns: 120
This is the same as example 0, but now the elevator can be ready at floor 30 when the employee arrives at time 100. It will still take 20 units of time to reach floor 50, dropping off the passenger at time 120.
2)

   
{10,120}
{1,100}
{210,200}
Returns: 230
The first employee will be picked up at time 10 on floor 1. At time 109, the elevator will arrive at floor 100. Here, it will wait 11 units of time until the second employee arrives and gets on (at time 120). It will then take another 100 units of time to reach floor 200 to drop off the second employee, and at time 230 it will finally arrive at floor 210 to drop off the first employee.
3)

   
{10,500}
{1,100}
{210,200}
Returns: 600
This is the same as example 2, but now the second employee doesn't arrive until time 500. In this case, it makes more sense for the elevator to drop the first employee off immedately, and then return to floor 100 to wait for the second employee.
4)

   
{1000,1200,1600,2000,2400}
{500,500,500,500,500}
{700,300,700,300,700}
Returns: 2600
The elevator should wait at floor 500 until time 2000, picking up the first four passengers as they arrive. It should drop off the second and fourth employees on floor 300, then return to floor 500 to pick up the fifth employee, and finally drop off the first, third, and fifth employees on floor 700.
5)

   
{775397,261225,870141,287698,884334}
{82225,958610,998971,413596,21922}
{769962,78706,477861,237168,258488}
Returns: 2724059

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

The goal of this problem is to determine the optimal strategy for your company's elevator. Given the arrival floors, arrival times, and destination floors of the elevator's users, you must return the shortest possible time in which the elevator can complete its task.

The constraints are what make this problem possible. There are at most five employees to consider, which should be a small enough number for even the slowest of brute-force algorithms to handle in 8 seconds.

To understand how to account for each case, let us consider an example where the maximum five employees all need to use the elevator. Your code should cycle through every permutation of "0011223344", where the first instance of a number signals picking up the corresponding employee, and the second instance signals dropping that employee off. Check the time required for each permutation, and the smallest value you find will be the answer.

C++ users have an advantage here with the ever-useful next_permutation() function. If your language doesn't have next_permutation() built-in, you should strongly consider writing one yourself and additing it to your code library for future use.

Alternately, this problem can be solved by recursion. Refer to the following pseudocode:

int timeWaiting(int[] passengerState, int currentFloor, int currentTime) {
if every passenger has been dropped off,
return currentTime
let answer = INFINITY
for each passenger p
if p has already been dropped off,
go on to the next passenger
otherwise, if p has not yet been picked up,
let newFloor = p's starting floor
let distance = abs(currentFloor - newFloor)
let newTime = max(p's arrival time, currentTime + distance)
let newPassengerState = passengerState with p now on elevator
let completionTime = timeWaiting(newPassengerState, newFloor, newTime)
otherwise, if p is currently in the elevator,
let newFloor = p's destination floor
let distance = abs(currentFloor - newFloor)
let newTime = currentTime + distance;
let newPassengerState = passengerState with p dropped off
let completionTime = timeWaiting(newPassengerState, newFloor, newTime)
if completionTime < answer
set answer = completionTime
return answer

你可能感兴趣的:(visio)