Problem 389
Asteroid Collision
Medium · 20 pts · Array, Stack, Simulation
Problem
Asteroids move along a line. Each one is given as an integer: its absolute value is its size, and its sign is its direction (positive moves right, negative moves left). All asteroids move at the same speed, so two asteroids collide only when a right-moving one is to the left of a left-moving one. In a collision the smaller asteroid explodes; if both are the same size, both explode. Print the asteroids that remain after all collisions, in their original order.
Input
First line: N. Second line: N non-zero integers.
Output
Print the remaining asteroids separated by spaces, or EMPTY if none remain.
Constraints
1 <= N <= 10000; -1000 <= A[i] <= 1000; A[i] != 0
Example input
3 5 10 -5
Example output
5 10
Explanation
In 5 10 -5, the asteroid -5 moves left into 10. 10 is larger, so -5 explodes. 5 and 10 both move right and never meet, so 5 10 remain.