D - "redocta".swap(i,i+1) Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400400

問題文

atcoder の並べ替えである文字列 SS が与えられます。
この文字列 SS に対して以下の操作を 00 回以上行います。

  • SS 中の隣接する 22 文字を選び、入れ替える。

SSatcoder にするために必要な最小の操作回数を求めてください。

制約

  • SSatcoder の並べ替えである文字列

入力

入力は以下の形式で標準入力から与えられる。

SS

出力

答えを整数として出力せよ。


入力例 1Copy

Copy
catredo

出力例 1Copy

Copy
8

catredo \rightarrow [ac]tredo \rightarrow actre[od] \rightarrow actr[oe]d \rightarrow actro[de] \rightarrow act[or]de \rightarrow acto[dr]e \rightarrow a[tc]odre \rightarrow atcod[er]
という流れで操作を行うと、 88 回で SSatcoder にすることができ、これが達成可能な最小の操作回数です。


入力例 2Copy

Copy
atcoder

出力例 2Copy

Copy
0

この場合、文字列 SS は元から atcoder です。


入力例 3Copy

Copy
redocta

出力例 3Copy

Copy
21

Score : 400400 points

Problem Statement

You are given a string SS that is a permutation of atcoder.
On this string SS, you will perform the following operation 00 or more times:

  • Choose two adjacent characters of SS and swap them.

Find the minimum number of operations required to make SS equal atcoder.

Constraints

  • SS is a string that is a permutation of atcoder

Input

Input is given from Standard Input in the following format:

SS

Output

Print the answer as an integer.


Sample Input 1Copy

Copy
catredo

Sample Output 1Copy

Copy
8

You can make SS equal atcoder in 88 operations as follows:
catredo \rightarrow [ac]tredo \rightarrow actre[od] \rightarrow actr[oe]d \rightarrow actro[de] \rightarrow act[or]de \rightarrow acto[dr]e \rightarrow a[tc]odre \rightarrow atcod[er]
This is the minimum number of operations achievable.


Sample Input 2Copy

Copy
atcoder

Sample Output 2Copy

Copy
0

In this case, the string SS is already atcoder.


Sample Input 3Copy

Copy
redocta

Sample Output 3Copy

Copy
21


2025-04-02 (Wed)
05:35:00 +00:00