#P8030. [COCI 2021/2022 #3] Ekoeko
[COCI 2021/2022 #3] Ekoeko
Description
You are given a positive integer and a string consisting of lowercase letters.
Swapping two adjacent letters in the string is considered one operation. Find the minimum number of operations needed to make the first lowercase letters of the original string exactly the same as the last lowercase letters.
Input Format
The first line contains a positive integer .
The second line contains a string of length consisting of lowercase letters. The testdata guarantees that each letter appears an even number of times.
Output Format
Output the minimum number of operations.
3
koeeok
3
3
kekoeo
1
4
soolnlsn
4
Hint
[Explanation for Sample 3]
One optimal sequence with the minimum number of operations:
$\texttt{soolnlsn} \to \texttt{so\red{lo}nlsn} \to \texttt{sol\red{no}lsn} \to \texttt{\red{os}lnolsn} \to \texttt{o\red{ls}nolsn}$
[Constraints and Notes]
This problem uses bundled subtasks.
- Subtask 1 (10 pts): The first letters of the string are all , and the last letters are all .
- Subtask 2 (20 pts): Each letter appears at most twice in the string.
- Subtask 3 (20 pts): The multiset of letters in the first positions is exactly the same as in the last positions, but the order may be different.
- Subtask 4 (20 pts): .
- Subtask 5 (40 pts): No special restrictions.
For of the testdata, .
[Hints and Additional Information]
Translated from COCI 2021-2022 CONTEST #3 Task 4 Ekoeko.
The scoring of this problem follows the original COCI problem settings, with a full score of .
Translated by ChatGPT 5
京公网安备 11011102002149号