CF1430E.String Reversal
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a string s. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string.
Your task is to calculate the minimum number of swaps you have to perform to reverse the given string.
给你一个字符串 s。你需要将其反转——即反转后第一个字母应等于反转前的最后一个字母,第二个字母应等于反转前的倒数第二个字母,以此类推。例如,若要将字符串 "abddea" 反转,则应得到字符串 "aeddba"。为实现这一目标,你可以交换字符串中相邻的字符。
你的任务是计算将给定字符串反转所需的最少交换次数。
输入格式
The first line contains one integer n (2≤n≤200000) — the length of s.
The second line contains s — a string consisting of n lowercase Latin letters.
第一行包含一个整数 n(2≤n≤200000)—— 字符串 s 的长度。
第二行包含字符串 s —— 一个由 n 个小写拉丁字母组成的字符串。
输出格式
Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string.
输出一个整数——为反转该字符串所需执行的相邻元素交换次数的最小值。
输入输出样例
输入#1
5 aaaza
输出#1
2
输入#2
6 cbaabc
输出#2
0
输入#3
9 icpcsguru
输出#3
30
说明/提示
In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps.
Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
在第一个例子中,你需要交换第三个和第四个元素,使字符串变为 "aazaa";然后交换第二个和第三个元素,使字符串变为 "azaaa"。因此,可以通过两次交换将该字符串反转。
由于第二个例子中的字符串本身就是一个回文串,因此无需进行任何操作即可将其反转。
输入解题思路,AI测评打分。不知道怎么写?