THFANDM - The flash and The moon

no tags 

Barry Allen is a police scientist (his job title was changed to a forensic scientist in The Flash: Iron Heights one-shot) with a reputation for being very slow, and frequently late, which frustrates his fiancee, Iris West, as the result of being absent-minded and his devotion to crime-solving. One night, as he is working late, a lightning bolt shatters a case full of chemicals and spills all over Barry. As a result, Allen finds that he can run extremely fast and has matching reflexes and senses. He dons a set of red tights sporting a lightning bolt (reminiscent of the original Fawcett Comics Captain Marvel), dubs himself the Flash (after his childhood comic book hero, Jay Garrick), and becomes Central City's resident costumed crimefighter. Central City University professor Ira West (Iris' adoptive father) designed Allen's costume and the ring which stores it while Allen is in his civilian identity. The ring can eject the compressed clothing when Allen needs it and suck it back in with the aid of a special gas that shrinks the suit. In addition, Allen invented the cosmic treadmill, a device that allowed for precise time travel and was used in many stories. Allen was so well liked that nearly all speedsters that come after him are often compared to him. Batman once said "Barry is the kind of man that I would've hoped to become if my parents had not been murdered."

Somehow, Barry lost his superspeed for some days so he is very upset.. He is alike simple men like us. At mid-night he goes to the rooftop of star-labs and looking to the sky.. He realized his eye sight is decreased to 45 degree left and 45 degree right only.. The moon is full and Barry loved to look at the moon. The moon is moving constantly from left to right with a constant velocity in a straight line. Barry wants to see the moon as much time as possible, so he needs to move.

Given the coordinates of the position of Barry where Barry is on the x axis… and the coordinate of the moon and the velocity of the moon, you need to calculate the displacement of Barry so that he can see the moon for certain given time.

Consider at the starting point Barry can see the moon…


The first line contains one integer N (1 <= N <= 10^5) denoting the number of test cases. Then N lines follows.

Each line containing five integers x, p, q, T and v:

  • x (-10^5 <= x <= 10^5): the coordinate of Barry with respect to x axis
  • T (1 <= T <= 10^3): the time Barry wants to see the moon
  • (p,q) (-10^5 <= p <= 10^5, 1 <= q <= 10^5): the coordinate of the moon
  • v (1 <= v <= 10^2): the velocity of the moon


Just print the total displacement of Barry if he wants to see the moon for T unit time for each test case.



5 5 6 5 2
0 1 1 5 1



hide comments
coderanant: 2018-01-07 11:43:41

i think some of the test case is wrong
That's why it's giving wrong answer for my absolutely correct code
Please check it @Sarwar.
Or please check the solution id-20936102 and devise me any test case for which it fails.

Last edit: 2018-01-07 11:54:16
prakash1108: 2018-01-06 10:48:13

@hastananda ....moon is moving from left to right.

Last edit: 2018-01-06 10:54:32
ai_zubair: 2018-01-04 18:57:13

@hanstananda @sarwar I'm using the p+v*T-x-q logic and getting right answers but it's still wrong answer during submission. Help please.

hanstan: 2018-01-04 15:35:37

Sorry but I'm neither the creator of the problem nor the SPOJ admin XD. Please ask the author @Sarwar for it.

ai_zubair: 2017-12-31 16:25:49

@hanstananda could you PLEASE provide some deciding test cases!

Last edit: 2017-12-31 16:32:44
vishalsingh17: 2017-12-31 13:18:16

@hanstananda can you plz check submission id ->> 20904553

Last edit: 2018-01-02 14:23:09
hanstan: 2017-12-31 07:22:58

Unofficial explanation (and assumptions used) from me XD:
1. I mapped this problem into xy-plane.
2. Consider Barry Allen is standing on the plane at x, y=0 at all times,.
3. The moon is a dot on a plane with coordinate (p,q).
4. T and v are as described above.
5. Barry Allen Vision is infinitely straight, with from 45 degree to the left to 45 degree to the right (which I assume then it is limited by these two lines: y1=x1-x, y2=-x2+x)
6. The output asked is the MINIMUM displacement.
7. Consider Allen can synchronize his speed with the speed of the moon regardless of the moon speed
Here we go the test cases XD
Tc 1: Moon is at (5,6). Then, after 5 time units, the new coordinate is(5,15). In order to see the moon, Allen has to move for 4 units from 5 to 9.
Tc 2: Moon is at (1,1). Then, after 5 time units, the new coordinate is(1,6). In order to see the moon, Allen has to move for 5 units from 0 to 5.

PS: Correct me for any mistakes XD

Last edit: 2017-12-31 07:45:15
[Rampage] Blue.Mary: 2017-12-30 11:49:33

Can you give the explanation of the sample input?

Added by:Sarwar
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)