CF1430E.String Reversal

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a string ss . 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.

输入格式

The first line contains one integer nn ( 2n2000002 \le n \le 200\,000 ) — the length of ss .

The second line contains ss — a string consisting of nn lowercase Latin letters.

输出格式

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.

首页