JOI Spring Training Camp 2015/16
Original Statement / Submission Site
Near the competition venue of the Japanese Olympiad in Informatics, there are two toilets: one is for women only, and the other is unisex. Women can use either toilet, but men can only use the unisex toilet.
After the competition, 2N participants are standing in a queue to use the toilets. Each participant is either male or female. Participants use the toilets in the following manner:
Each participant takes exactly 1 minute to use the toilet. Time taken to walk to the toilet is ignored.
The goal is to rearrange the initial queue such that all participants finish using the toilet within N minutes.
For any rearrangement of the queue, the dissatisfaction of a participant is defined as the number of people who were originally behind them but are now ahead of them in the rearranged queue. (Note: Changes of order due to toilet usage during execution are not counted towards dissatisfaction.)
We want to minimize the maximum dissatisfaction among all participants, under the condition that all participants finish using the toilet within N minutes.
Given information about the 2N participants in the queue, determine whether it is possible to rearrange them so that all finish using the toilets within N minutes. If it is possible, compute the minimum possible value of the maximum dissatisfaction among the participants.
Print the minimum possible maximum dissatisfaction value. If no rearrangement can allow all participants to finish within N minutes, print -1.
6 1 FFFMMMMMMFFF 1
2
There are 12 participants. We can arrange them like this:
FMMFFMMMMFFF
This arrangement allows all participants to finish within 6 minutes, and the maximum dissatisfaction is 2.
6 1 MMFFMMMMFFMF 1
-1
No rearrangement can allow everyone to finish within 6 minutes.
6 1 MFFFMFMMFFFM 1
0
6 4 M 1 F 2 FM 2 MFFFM 1
0
Note that sample inputs 3 and 4 describe exactly the same queue.