I. String Reversal

Codeforces
IDCF10279I
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
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. The first line contains one integer $n$ ($2 <= n <= 200 thin 000$) — the length of $s$. The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. 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. ## Input The first line contains one integer $n$ ($2 <= n <= 200 thin 000$) — the length of $s$.The second line contains $s$ — a string consisting of $n$ lowercase Latin letters. ## Output Print one integer — the minimum number of swaps of neighboring elements you have to perform to reverse the string. [samples] ## Note 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.
**Definitions** Let $ s = s_1 s_2 \dots s_n $ be a string of length $ n $, where each $ s_i \in \Sigma $, $ \Sigma $ is the set of lowercase Latin letters. Let $ s^R = s_n s_{n-1} \dots s_1 $ be the reverse of $ s $. **Constraints** $ 2 \leq n \leq 200{,}000 $ **Objective** Compute the minimum number of adjacent swaps required to transform $ s $ into $ s^R $.
API Response (JSON)
{
  "problem": {
    "name": "I. String Reversal",
    "description": {
      "content": "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 l",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10279I"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 l...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ s = s_1 s_2 \\dots s_n $ be a string of length $ n $, where each $ s_i \\in \\Sigma $, $ \\Sigma $ is the set of lowercase Latin letters.  \nLet $ s^R = s_n s_{n-1} \\dots s_1 $ be t...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments