Submit | All submissions | Best solutions | Back to list |
ELEVTRBL - Elevator Trouble |
You are on your way to your first job interview as a program tester, and you are already late. The interview is in a skyscraper and you are currently in floor s, where you see an elevator. Upon entering the elevator, you learn that it has only two buttons, marked "UP u" and "DOWN d". You conclude that the UP-button takes the elevator u floors up (if there aren't enough floors, pressing the UP-button does nothing, or at least so you assume), whereas the DOWN-button takes you d stories down (or none if there aren't enough). Knowing that the interview is at floor g, and that there are only f floors in the building, you quickly decide to write a program that gives you the amount of button pushes you need to perform. If you simply cannot reach the correct floor, your program halts with the message "use the stairs".
Given input f, s, g, u and d (floors, start, goal, up, down), find the shortest sequence of button presses you must press in order to get from s to g, given a building of floors, or output "use the stairs" if you cannot get from s to g by the given elevator.
Input
The input will consist of one line, namely f s g u d, where 1 <= s, g <= f <= 1000000 and 0 <= u, d <= 1000000. The floors are one-indexed, i.e. if there are 10 stories, s and g be in [1; 10].
Output
You must reply with the minimum numbers of pushes you must make in order to get from s to g, or output "use the stairs" if it is impossible given the conguration of the elevator.
Example
Input: 10 1 10 2 1 Output: 6
Input: 100 2 1 1 0 Output: use the stairs
Added by: | Krzysztof Lewko |
Date: | 2011-10-06 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | Nordic programming contest |
hide comments
|
||||||||||
2024-11-23 10:28:48
beware of tc g = s so ans is 0 |
||||||||||
2022-01-22 17:21:32
Done with BFS in one go |
||||||||||
2021-06-24 17:00:56
bfs ... n check for s==g tc : 11 ;) |
||||||||||
2021-02-13 18:39:12
those who are getting TLE, you must check the size of arrays UwU...(give size very large , s.t. it is large enough to cover all the values...) |
||||||||||
2020-12-31 17:49:33
AC in one go!!! bfs |
||||||||||
2020-12-13 14:52:17
Thanks, @ak8325 :-) |
||||||||||
2020-11-04 12:15:27
Don't know why no one is talking about simple use of DP in this question! BTW ac in one go with DP! |
||||||||||
2020-10-16 18:57:22
Exmple For TestCase 12 : 10 8 3 2 1 o/p -- 5 Last edit: 2023-05-19 15:38:46 |
||||||||||
2020-08-23 18:54:50
thanks @prateekpandey :) |
||||||||||
2020-06-16 23:58:29
AC in one go. SImple bfs. No need to construct graph! |