#P1878. Dance Pairing Simulation
Dance Pairing Simulation
Dance Pairing Simulation
There are n people attending a dance lesson. Each person has a dance skill represented by an integer. Initially, they stand in a row from left to right. In each step, if there is at least one pair of adjacent persons of opposite genders, the pair among them whose dance skills have the minimum absolute difference (i.e. \(|a_i - a_j|\)) will leave the line and start dancing. If there is more than one such pair (i.e. several pairs with the same minimum absolute difference), then the leftmost pair (i.e. the one that appears earliest in the row) is chosen. Once a pair leaves, the gap is closed by shifting the remaining people while preserving their order. The process continues until no adjacent pair of opposite genders exists. Your task is to simulate this process and determine the pairing and the order in which pairs start dancing.
Note: The absolute difference between two dance skills \(a\) and \(b\) is computed as \(|a - b|\).
inputFormat
The input consists of three lines:
- The first line contains an integer \(n\) representing the number of participants.
- The second line contains a string of length \(n\) composed of characters 'M' and 'F', where 'M' stands for male and 'F' stands for female. This string represents the gender of each participant in the initial order.
- The third line contains \(n\) space-separated integers, where the \(i\)-th integer represents the dance skill of the \(i\)-th person.
outputFormat
For each dancing pair (in the order they were chosen), output a line containing the two integers (dance skills) of the paired participants in the order they appear in the current row at the time of pairing.
If no pair can be formed at any step then nothing should be printed for that step.
sample
5
MFFMF
10 13 5 8 7
8 7
10 13
</p>