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 ;
}

代码

Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心的更多相关文章

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

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

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

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

  3. Codeforces Round #277 (Div. 2) 题解

    Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...

  4. 【codeforces】Codeforces Round #277 (Div. 2) 解读

    门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...

  5. Codeforces Round #277(Div 2) A、B、C、D、E题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. Calculating Function 水题,判个奇偶即可 #includ ...

  6. Codeforces Round #277 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...

  7. Codeforces Round #277 (Div. 2) A B C 水 模拟 贪心

    A. Calculating Function time limit per test 1 second memory limit per test 256 megabytes input stand ...

  8. 套题 Codeforces Round #277 (Div. 2)

    A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...

  9. 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. ...

随机推荐

  1. yield让代码更加简洁

    不能传入out或ref public IEnumerable<Shop> GetShop() { ; i < ; i++) { yield return new Shop { ID ...

  2. 模拟测试—moq:简单一两句

    在Xunit的基础上,说话模拟测试. 假如我们有这样一个控制器里面有这样一个方法,如图 我们在对Bar测试得时候,如果测试未通过,错误有可能来至于Bar,也有可能错误来至于serverde Foo方法 ...

  3. html——导航demo

    通过行内块.伪类对导航栏进行设置 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  4. mysql常用命令介绍

    mysql适用于在Internet上存取数据,支持多种平台 1.主键:唯一标识表中每行的这个列,没有主键更新或删除表中的特定行很困难. 2.连接mysql可以用Navicat 要读取数据库中的内容先要 ...

  5. 大白_uva10795_新汉诺塔

    题意:给出所有盘子的初态和终态,问最少多少步能从初态走到终态,其余规则和老汉诺塔一样. 思路: 若要把当前最大的盘子m从1移动到3,那么首先必须把剩下的所有盘子1~m-1放到2上,然后把m放到3上. ...

  6. UITableview 兼容IOS6 和IOS7的方法

    1. TableVIew向下拉44像素  添加Auto layout 2. Extended edge 选择Under top bars 2. 在Viewdidload中添加代码 if ([[UIDe ...

  7. Redis事物及锁的运用

    redis与mysql事物比较如下: 下面是一个redis事物运用于买票的demo

  8. CAD实现自定义实体夹点移动(com接口VB语言)

    主要用到函数说明: MxDrawXCustomEvent::MxDrawXCustomEntity::moveGripPointsAt 自定义实体事件,自定义实体夹点被移动,详细说明如下: 参数 说明 ...

  9. es6-let/var/const

    const和var区别 for(let i=0;i<3;i++) { console.log(i); } console.log(i); for(var i=0;i<3;i++) { co ...

  10. vue-cli3 中的环境变量

    官方文档是这样写的: src同名文件夹下的建立 .env.[model] 配置文件  // mode:production development ... 载入的变量会对vue-cli-service ...