#FT0000066. Đảo ngược chuỗi bằng đổi chỗ kề (String Reversal)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đảo ngược chuỗi bằng đổi chỗ kề (String Reversal)
String Reversal
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a string of lowercase English letters. Transform it into the reversal of the original string, so the first character becomes the last, the second becomes the second-to-last, and so forth.
In one operation you may swap characters at adjacent positions. You may perform any number of operations, including zero for an already palindromic string.
Find the minimum number of adjacent swaps needed to obtain the exact reversed string. Equal letters may occur multiple times and need not be distinguished in the final string.
Input
The first line contains , the length of the string.
The second line contains , exactly lowercase letters a–z.
Output
Print one integer: the minimum number of adjacent character swaps.
Subtasks
-
Subtask 1 (20 points): ; ; each character of is in
a–z. -
Subtask 2 (30 points): ; ; each character of is in
a–z. -
Subtask 3 (50 points): ; ; each character of is in
a–z.
Examples
Input
5
aaaza
Output
2
Explanation
The target is the reverse of aaaza, namely azaaa.
- Swap positions and :
aaazabecomesaazaa. - Swap positions and :
aazaabecomesazaaa.
Two swaps are sufficient. The only z must move from position to position , and an adjacent swap moves it by at most one position; therefore fewer than two swaps cannot work. The answer is 2.