ELEVTRBL - Elevator Trouble

no tags 

You are on your way to your rst job interview as a program tester, and you are already
late. The interview is in a skyscraper and you are currently in
oor s, where you see an
elevator. Upon entering the elvator, 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
oors, pressing the UP-botton 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), nd 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 con guration of the elvator.

Example

Input:
10 1 10 2 1
Output:
6

Added by:Krzysztof Lewko
Date:2011-10-06
Time limit:0.148s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All
Resource:Nordic programming contest

hide comments
ADITYA JAIN: 2015-07-31 16:15:10

My 100th :)

Saksham : 2015-07-27 14:00:26

BFS rocks!! AC in 1 go

Abhinandan Agarwal: 2015-07-27 10:13:05

Simple BFS gives 0.00s ! :-)

Sumit Paroothi: 2015-07-26 16:33:01

multiple test files, beware !

chin: 2015-06-28 22:31:02

Finally AC..:D

Aditya Joshi: 2014-11-29 06:04:35

HINT:


For people using Java, try and use an ArrayDeque instead of a LinkedList to implement your abstract Queue.

Last edit: 2014-11-29 06:04:45
chk: 2013-09-06 17:35:30

awesome problem :)

Rahul Dubey: 2012-09-22 11:46:49

take care of the case if(s==g ) output is 0; it caused me 5-6 wrong answer :(:(
Also Test cases are too weak.. wrong solution getting accepted :P

Last edit: 2012-09-30 21:40:27
Ajey Golsangi: 2012-07-21 11:36:39

Clear problem description found here http://ncpc.idi.ntnu.no/ncpc2011/ncpc2011problems.pdf

:D: 2012-07-16 11:08:06

I just rechecked my old submission and it still passes with roughly the same time. The problem is ok and your solution is simply to weak. It's not the first time when the author's solution is too weak for SPOJ standards ;)