Ant Movement
Time limit1sMemory limit128 MB
Simulate ants that reverse on collision and fall off the plank, and report the fall time and the original position(s) of the last ants.
- Level
Medium6 of 10
- Topics
- Simulation, Sorting, Math, Implementation
- Solved
- No attempts yet
Problem
ants are marching on a straight wooden plank. Each ant faces either left or right and moves 1 cm per second in the direction it faces.
- When two ants meet at the same point, both immediately reverse direction and move the opposite way.
- When an ant reaches either end of the plank (position or position ), it falls to the ground and no longer affects any other ant.
- The size of an ant is negligible.
For example (the original statement came with a figure of this situation), starting at time : after second ants E and A meet at position and reverse direction. After seconds A and B meet while C and D also meet, so all four ants reverse. After another second (i.e. at time seconds) ant E reaches the end of the plank and falls to the ground.
Write a program that simulates the movement of the ants.
Input
The input consists of several test cases. The first line of each test case contains the length of the plank (in cm, ) and the number of ants ().
Each of the following lines contains an ant's position () and the direction it faces (L: left, R: right). No two ants share the same position.
The input continues until the end of the file.
Output
For each test case, print one line in the following format:
The last ant will fall down in T seconds - started at P.
Here is the time at which the last ant fell, and is the position where that ant was located at time . If two ants fall at the same time, print started at P and Q instead of started at P, where .