CF486C.Palindrome Transformation

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.

There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).

When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.

Initially, the text cursor is at position p.

Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?

Nam 正在电脑上操作一个字符串。该字符串由 n 个小写英文字母组成。这个字符串毫无意义,因此 Nam 决定通过使用四个方向键(左、右、上、下)使其变得更“优美”,即将其变为一个回文串。

字符串中有一个光标指向某个字符。假设当前光标位于位置 i(1 ≤ i ≤ n,字符串采用从 1 开始的索引)。左右方向键用于在字符串中移动光标。字符串是循环的:当 Nam 按下左方向键时,若 i > 1,则光标移至位置 i − 1;否则(即 i = 1),光标移至字符串末尾(即位置 n)。按下右方向键时同理(若 i = n,则光标移至字符串开头)。

当 Nam 按下上方向键时,光标所指向的字母将变为英文字母表中的下一个字母(假设字母表也是循环的,即 'z' 的下一个字母是 'a')。按下下方向键时同理。

初始时,光标位于位置 p。

由于 Nam 还有很多作业要做,他希望尽快完成这一任务。你能帮他计算出使字符串变为回文串所需的最少按键次数吗?

输入格式

The first line contains two space-separated integers n (1 ≤ n ≤ 105) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.

The next line contains n lowercase characters of Nam's string.

第一行包含两个以空格分隔的整数 nn(1 ≤ n ≤ 1051 ≤ n ≤ 10^5)和 pp(1 ≤ p ≤ n1 ≤ p ≤ n),分别表示 Nam 字符串的长度以及文本光标的初始位置。

下一行包含 Nam 字符串的 nn 个小写字符。

输出格式

Print the minimum number of presses needed to change string into a palindrome.

输出将字符串变为回文串所需的最少按键次数。

输入输出样例

  • 输入#1

    8 3
    aeabcaez

    输出#1

    6

说明/提示

A string is a palindrome if it reads the same forward or reversed.

In the sample test, initial Nam's string is: (cursor position is shown bold).

In optimal solution, Nam may do 6 following steps:

The result, , is now a palindrome.

如果一个字符串正读和反读都相同,则该字符串是回文串。

在样例测试中,Nam 的初始字符串为:(光标位置以粗体显示)。

在最优解中,Nam 可执行以下 6 步操作:

最终结果 现在是一个回文串。

输入解题思路,AI测评打分。不知道怎么写?

首页