#P10349. Ant Collisions
Ant Collisions
Ant Collisions
This problem is adapted from the trial round of PA 2024 (Mrówki).
On a number line there are $n$ ants. The $i$-th ant is initially located at point $x=i$. Each ant faces either to the right (the positive direction) or to the left (the negative direction). We can treat each ant as a point. Once a signal is given, every ant begins to move at unit speed in the direction it is facing. When two ants collide (i.e. they meet at the same point), they bounce off each other – that is, they both immediately reverse their direction and continue moving.
It can be proven that after a finite time no further collisions will occur. Your task is to compute for each ant the total number of collisions it will experience with the other ants.
Note: Using physical analysis it can be shown that the collisions between ants have the same effect as if the ants were allowed to pass through each other (with appropriate swapping of identities). However, each collision still counts for both ants involved.
In mathematical terms, if we denote the velocity of an ant by $v\in\{+1,-1\}$ and its initial position by $x=i$, then after the signal the position evolves according to $$x_i(t)=i+v_i\,t.$$ When two ants, say the $i$-th and $j$-th, meet (i.e. $x_i(t)=x_j(t)$) and if at that moment one is moving right and the other left, they collide, both incrementing their collision count by $1$, and then reverse directions. The process is repeated until no further collisions occur.
Your program should determine the number of collisions each ant (identified by its initial index from $1$ to $n$) undergoes.
inputFormat
The input consists of two lines.
- The first line contains an integer $n$ ($1 \le n \le 1000$) representing the number of ants.
- The second line contains a string of length $n$. The $i$-th character is either
R
orL
, indicating that the ant at position $x=i$ initially faces right (ifR
) or left (ifL
).
outputFormat
Output a single line containing $n$ integers separated by spaces. The $i$-th integer should be the total number of collisions experienced by the ant initially at position $x=i$.
sample
3
RLL
1 2 1
</p>