C. Palindrome Transformation
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output

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的串、一个初始位置,每次可以左移一格、右移一格、把当前这一格位置上的字母upcase、downcase,求把它变成回文串的最小步数

注意到回文串是对称的,所以你在i位置修改和在n-i+1位置修改是一样的,代价不变

所以只要考虑位置移动的代价就好了

显然只修改前半边的字母或者只修改后半边的字母是最优的

然后只要考虑一下从m出发先去l再去r优还是先去r再去l优,自己推一推就好了

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
int cost[1000010];
char c[1000010];
int n,m,l=inf,r=0,tot=0;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void ex()
{
printf("NO");
exit(0);
}
int main()
{
n=read();m=read();
if (m>n/2)m=n-m+1;
for (int i=1;i<=n;i++)
{
char ch=getchar();
while (ch<'a'||ch>'z')ch=getchar();
c[i]=ch;
}
for (int i=1;i<=n/2;i++)
{
cost[i]=min(abs(c[i]-c[n-i+1]),abs(c[i]+26-c[n-i+1]));
cost[i]=min(cost[i],abs(c[i]-26-c[n-i+1]));
tot+=cost[i];
}
for (int i=1;i<=n/2;i++)
{
if (cost[i])
{
l=min(l,i);
r=max(r,i);
}
}
if (tot==0)printf("0");
else
printf("%d\n",tot+r-l+min(abs(m-r),abs(m-l)));
}

  

cf486C Palindrome Transformation的更多相关文章

  1. Codeforces 486C Palindrome Transformation(贪心)

    题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...

  2. Codeforces Round 486C - Palindrome Transformation 贪心

    C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...

  3. codeforces 486C Palindrome Transformation 贪心求构造回文

    点击打开链接 C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes ...

  4. Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)

    Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

    题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...

  6. Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心

    C. Palindrome Transformation     Nam is playing with a string on his computer. The string consists o ...

  7. Codeforces Round #277(Div. 2) (A Calculating Function, B OR in Matrix, C Palindrome Transformation)

    #include<iostream> #include<cstring> #include<cstdio> /* 题意:计算f(n) = -1 + 2 -3 +4. ...

  8. codeforces 486C. Palindrome Transformation 解题报告

    题目链接:http://codeforces.com/problemset/problem/486/C 题目意思:给出一个含有 n 个小写字母的字符串 s 和指针初始化的位置(指向s的某个字符).可以 ...

  9. CodeForces 486C Palindrome Transformation 贪心+抽象问题本质

    题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...

随机推荐

  1. j2ee开源项目——IT学习者博客(itxxzblog v1.0)

    大家好,我是IT学习者-螃蟹,已经有近一周的时间没有更新文章了,作为回报,今天起将更新一个大件,也就是螃蟹还在进行中的IT学习者博客. IT学习者博客的初期设计已经完成,功能也已经完成了大半,具备了当 ...

  2. [置顶] 顿悟JAVA,自己实现Object的Clone的约束关系(上)

    因protected 的理解,顿悟一些JAVA的原理,模拟了Object类的子类为什么在调用clone方法前实现Cloneable接口. 这里不解释 ,上代码先. 运行效果 文件结构 调用类 pack ...

  3. oracle 格式化数字 to_char

    转:http://blog.csdn.net/chinarenzhou/article/details/5748965 Postgres 格式化函数提供一套有效的工具用于把各种数据类型(日期/时间,i ...

  4. [Cycle.js] Introducing run() and driver functions

    Currently the code looks like : // Logic (functional) function main() { return { DOM: Rx.Observable. ...

  5. 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...

  6. Android 自定义UI--指南针

    有了之前的基础,下面开始实现一个简单的指南针.首先来看一下效果图, 我们可以粗略将这个指南针分为三个部分,一是圆形背景,二是刻度,三是文本.那么在写代码的时候,就可以声明三个Paint画笔来画以上三个 ...

  7. oracle监听

    启动实例时,监听程序进程会建立一个指向Oracle DB 的通信路径.随后,监听程序可接受数据库连接请求.使用监听程序控制实用程序可控制监听程序.使用lsnrctl,可以:• 启动监听程序• 停止监听 ...

  8. IOS6和IOS7的屏幕适配问题

    自从IOS7出来以后,以前写在IOS6上或者更低版本的程序,跑在IOS7的模拟器上就会出现一些问题.最大的问题就是,所有的UI空间都会统一向上移动20个点(如果空间的y值为0,就会被StatusBar ...

  9. C#遍历Object各个属性含List泛型嵌套。

    同事遇到一个问题:在做手机app接口时,返回JSON格式,json里面的数据属性均是string类型,但不能出现NULL(手机端那边说处理很麻烦,哎).Model已经创建好了,而且model的每个属性 ...

  10. HTML与CSS入门——第十二章  在网页中使用多媒体

    知识点: 1.如何链接多媒体文件 2.如何嵌入多媒体文件 3.使用多媒体的更多技巧 多媒体文件:音频,视频和动画,以及静态的图像和文本. 这里我就直接讲HTML5了…… 此前都是用ojbect来加载或 ...