TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

ElevatorBank

Problem statement, definition, constraints, and public examples.

Problem Statement

Typically, office buildings have more than just one elevator, for convenience. After all, when you push the button on the ground floor, who really wants to wait a minute for the one elevator in the building to make its way all the way down? However, when you have multiple elevators, collisions may start to occur, where multiple elevators go for the same button press. You have been hired to program new, efficient software for the elevator controller.

The problem is that the elevator must respond on command to the various button presses and cannot know when people will press the button or which buttons will be pressed. Thus, you have decided that the best way to go is to treat each passenger as if he were the final passenger you would have to deal with for the day, no matter whether he is or not.

At time 0 seconds, each elevator starts at floor 1, the ground floor, and is motionless. Each elevator keeps a list of floors that it must stop at, and cannot reverse direction until all of the floors on the list are in the opposite direction to the direction that it is traveling in. When a passenger pushes the call button on some floor, exactly one elevator will be dispatched to that location, the one that would arrive there first of all of the elevators. If there is a tie, dispatch the fastest elevator. If there is still a tie, then dispatch the lowest-indexed elevator.

The process of dispatching works as follows: if the dispatched elevator is motionless, then it adds the relevant floor to its list and moves in the correct direction. Otherwise, it simply adds the floor to its list and keeps moving in the same direction as it was already. If an elevator is motionless and two floors in opposite directions are added to its queue simultaneously, then it should move towards the closer one. If there is no unique closest floor, then set the direction of motion downwards (towards lower-numbered-floors).

When an elevator arrives at a floor that is on its list, it drops off and picks up all the passengers that wish to leave or enter at that floor, respectively, and then it crosses the floor off its list. If passengers are getting on, then the floors they wish to go to are added to that elevator's list. When a given elevator's list contains no more floors, it stops moving.

The elevators may have been installed or refurbished at different times, and thus may have different speeds. secondsPerFloor is a int[] representing the speeds of elevators 0, 1, ... in seconds per floor traveled. passengers is a String[] containing some number of commands from passengers. Each String represents one passenger, and is of the form "CALL f t TO d", where f is the floor that the passenger is at, t is the time at which the call button is pressed, in seconds from the start of the day, and d is the destination floor of the passenger. It is guaranteed that f will not be the same as d, that f and d will be between 1 and 200, inclusive, and that t will be between 1 and 50000, inclusive. In addition, none of them will contain leading zeros.

Before it is put into production, the software must be tested. There are a number of simulated passengers that will be used to test your elevator controller program. Your method should run the elevator controller for the given set of passengers and return an int that represents the number of seconds it will take for all of the passengers to get to their destinations.

Definition

Class:
ElevatorBank
Method:
timeNeeded
Parameters:
int[], String[]
Returns:
int
Method signature:
int timeNeeded(int[] secondsPerFloor, String[] passengers)
(be sure your method is public)

Notes

  • Note that when a passenger pushes the button and dispatches an elevator to pick them up, the elevator that passenger will get on may not be the elevator that was dispatched. However, the dispatched elevator will still travel to that floor.

Constraints

  • secondsPerFloor will contain between 1 and 5 elements, inclusive.
  • each element of secondsPerFloor will be between 1 and 100, inclusive.
  • passengers will contain between 0 and 50 elements, inclusive.
  • each element of passengers will be in the format "CALL f t TO d", as listed in the problem statement.
  • f will be between 1 and 200, inclusive, and will not contain leading zeros
  • t will be between 1 and 50000, inclusive, and will not contain leading zeros
  • d will be between 1 and 200, inclusive, and will not contain leading zeros
  • f will not be equal to d

Examples

  1. {50,100}
    {"CALL 1 10 TO 20",
     "CALL 5 50 TO 1"}
    Returns: 1910
    At t = 10 seconds, the first passenger pushes the call button on floor 1, and he gets on elevator 0 immediately. Elevator 0 starts going up to floor 20. At t = 50 seconds, the second passenger pushes the call button on floor 5, and since elevator 0 would get there faster than elevator 1, it gets dispatched. Then elevator 0 picks up the passenger on floor 5, drops off one passenger on floor 20, and goes back down to floor 1 to drop off the last passenger.
  2. {2,4}
    {"CALL 1 10 TO 20",
     "CALL 5 50 TO 1"}
    Returns: 82
    Same ratio of speeds, but this time, elevator 0 will get to floor 20 exactly when the button is pushed on floor 5, so elevator 1 will be dispatched for the passenger instead.
  3. {1,6,3,4,2}
    {"CALL 5 19 TO 11", "CALL 7 76 TO 1", "CALL 1 1 TO 5",
     "CALL 9 60 TO 24", "CALL 16 45 TO 3", "CALL 7 70 TO 9"}
    Returns: 102
  4. {5}
    {"CALL 1 10 TO 20", "CALL 19 170 TO 15", "CALL 21 170 TO 25"}
    Returns: 245
    The first passenger sends the elevator to floor 20. Then at t = 170 seconds, the elevator is dispatched simultaneously for both floors 19 and 21. The tiebreaker sends it downwards.
  5. {5}
    {"CALL 1 10 TO 20", "CALL 16 170 TO 25", "CALL 21 170 TO 25"}
    Returns: 285
    The elevator goes to floor 20 as in the last case. This time, it is dispatched to floors 16 and 21 simultaneously, and floor 21 is closer, so it goes there first.
  6. {17, 22, 41, 16, 21}
    {"CALL 5 10 TO 19", "CALL 25 86 TO 21", "CALL 26 159 TO 9",
     "CALL 100 400 TO 3", "CALL 7 215 TO 38", "CALL 14 90 TO 17",
     "CALL 25 680 TO 2", "CALL 1 1400 TO 99", "CALL 96 961 TO 12"}
    Returns: 3458
  7. {100}
    {}
    Returns: 0
Back to all problems