C. Palindrome Transformation
 
 

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?

Input

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.

Output

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

Sample test(s)
input
8 3
aeabcaez
output
6
Note

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.

题意:给你一个长度n的字符串和光标的起始位置,

再给出以下4种操作 光标左移 光标右移 字符上换 字符下换。 问将给出的字符换成回文串的最小花费是多少。

题解:可以先预处理出每个地方的光标上下变换次数,再贪心求步数最小

我们当然贪心在一半边内移动

///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define meminf(a) memset(a,127,sizeof(a)); inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
#define maxn 100000+50
#define inf 1000000007 int main(){
int n,k;
char a[maxn];
int G[maxn];
bool bo=;
scanf("%d%d",&n,&k);getchar();
for(int i=;i<=n;i++){
scanf("%c",&a[i]);
}
int l=,r=n,pos,next[maxn];
if(k<=n/)pos=;
else pos=;
int kk=;G[]=-inf;
while(l<=r){
if(a[l]!=a[r]){
if(pos==){
G[++kk]=r;
if(r==k)bo=;
next[r]=l;
}
else{
if(l==k)bo=;G[++kk]=l;next[l]=r;
}
} l++,r--;
}
if(!bo){
G[++kk]=k;
}
sort(G+,G+kk+);
int ans=;
int last;//cout<<kk<<endl;
int fa=lower_bound(G+,G+kk+,k)-G;
if(abs(G[]-G[fa])>=abs(G[kk]-G[fa])){
last=G[fa];//cout<<last<<" "<<G[fa]<<endl;
for(int i=fa+;i<=kk;i++){
ans+=min(-abs(a[G[i]]-a[next[G[i]]]),abs(a[G[i]]-a[next[G[i]]]));;
ans+=abs(G[i]-last);last=G[i];
}last=G[kk];//cout<<ans<<endl;
for(int i=fa-;i>=;i--){
ans+=min(-abs(a[G[i]]-a[next[G[i]]]),abs(a[G[i]]-a[next[G[i]]]));
ans+=abs(G[i]-last);last=G[i];
}//cout<<ans<<endl;
}
else {
last=G[fa];
for(int i=fa-;i>=;i--){
ans+=min(-abs(a[G[i]]-a[next[G[i]]]),abs(a[G[i]]-a[next[G[i]]]));
ans+=abs(G[i]-last);;last=G[i];
} last=G[];
for(int i=fa+;i<=kk;i++){
ans+=min(-abs(a[G[i]]-a[next[G[i]]]),abs(a[G[i]]-a[next[G[i]]]));
ans+=abs(G[i]-last);;last=G[i];
}
}if(bo)ans+=min(-abs(a[k]-a[next[k]]),abs(a[next[k]]-a[k]));
cout<<ans<<endl; return ;
}

代码

最新文章

  1. 【探索】在 JavaScript 中使用 C 程序
  2. Bzoj3450 Tyvj1952 Easy
  3. 模块化开发--sea.js
  4. C#封装好的Win32API
  5. Spring 4 官方文档学习(十四)WebSocket支持
  6. LightOj1383 - Underwater Snipers(贪心 + 二分)
  7. 第五章 管理程序流(In .net4.5) 之 异常处理
  8. Mime Types
  9. thinkphp 定制错误页面
  10. 创建mvc
  11. Delphi XE7中新并行库
  12. c#怎样获取excel单元格的RGB颜色
  13. 记录在IIS中安装部署Orchard遇到的问题
  14. HDU 4081 Qin Shi Huang&#39;s National Road System 次小生成树变种
  15. 转:【Java并发编程】之四:守护线程与线程阻塞的四种情况
  16. Codeforces round 1086
  17. AFN拿不到cookie,无法存储cookie
  18. 2017-2018-2 20165215 实验二 Java面向对象程序设计
  19. 小米2s线刷出现remote: partition table doesn&#39;t exist
  20. 实验五 Java网络编程及安全 实验报告 20135232王玥

热门文章

  1. Struts2之一 初体验
  2. Java&amp;Xml教程(四)使用DOM方式生成XML文件
  3. node.js安装步骤
  4. Intent的调用
  5. 如何防止SQL注入式攻击
  6. 【sqli-labs】 less44 POST -Error based -String -Stacked Blind(POST型基于盲注的堆叠字符型注入)
  7. switch方法中使用数字区间
  8. Fedora 和 RedHat 以及 SUSE 中 YUM 工具的使用
  9. iPhone设备当前IP和SSID的获取
  10. jquery制作动态添加表单行与删除表单行