Ants
InterviewTime limit1sMemory limit1024 MB
Given n ants on a line, each facing left or right, count how many times each ant bounces off other ants when they collide and reverse.
- Level
Medium6 of 10
- Topics
- Prefix sum, Simulation
- Solved
- No attempts yet
Statement
There are ants on a number line, and the -th ant stands at point . Each ant faces either right (toward increasing coordinates) or left (toward decreasing coordinates). The ants are small enough to be treated as points.
Process
At the start signal, every ant walks in the direction it faces, all at the same speed of 1 unit. When two ants meet at the same point, they bounce off each other: both reverse direction and keep walking. It can be proven that after some time no more collisions happen. Write a program that computes, for each ant, how many times it bounces off other ants.
Input
The first line contains one integer (), the number of ants. The second line contains a string of length made only of the characters L and P. If the -th character is L, the -th ant initially faces left. Otherwise, if it is P, the ant faces right.
Output
Print integers separated by single spaces. The -th integer is the number of bounces of the -th ant.
Hint
In the example, the first ant initially faces left and never bounces off any other ant. The last ant collides with the fifth ant at point 5.5, then walks right and never stops. The third ant bounces off the fourth ant at point 3.5 and then walks left. The second ant bounces off it at point 3, then turns left and never stops walking.