You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
876 B
C++
31 lines
876 B
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
|
|
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 1 << 20;
|
|
char buf[N];
|
|
int diff[N];
|
|
int calc(char a, char b)
|
|
{
|
|
if (a > b) swap(a, b);
|
|
return min(b - a, a + 26 - b);
|
|
}
|
|
int main()
|
|
{
|
|
int n, p;
|
|
scanf("%d%d%s", &n, &p, buf);
|
|
p--;
|
|
if (p >= (n >> 1)) p = n - p - 1, reverse(buf, buf + n);
|
|
for (int i = 0; i < (n >> 1); i++) diff[i] = calc(buf[i], buf[n - i - 1]);
|
|
int L = p, R = p;
|
|
for (int i = p; i >= 0; i--)
|
|
if (diff[i]) L = i;
|
|
for (int i = p; i < (n >> 1); i++)
|
|
if (diff[i]) R = i;
|
|
int base_ans = accumulate(diff + L, diff + R + 1, 0);
|
|
if (L == p) return printf("%d", base_ans + R - p), 0;
|
|
if (R == p) return printf("%d", base_ans + p - L), 0;
|
|
printf("%d", base_ans + R - L + min(R - p, p - L));
|
|
return 0;
|
|
}
|