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. css文本背景样式

    文本样式 文本类 text-transform:uppercase: 全部变为大写 text-transform:lowercase: 全部变为小写 text-transform:capitalize ...

  2. Android互动设计-蓝牙遥控自走车iTank

    一.让Android与外部的设备互动 iTank智能型移动平台基本款简介 iTank智能型移动平台是一台履带车,车体上方的控制板有一颗微处理器,我们可以通过它的UART或是I2C接口下达指令来控制iT ...

  3. 重现apache commons fileupload DOS漏洞

    这个漏洞是2014年2月4日被发现的, 因为该组件试用范围非常广, 所以该漏洞的影响也非常巨大.通过特制的包含畸形header的http请求,可以导致使用该组件的应用程序进入无限循环从而耗尽CPU等资 ...

  4. JS高级——变量提升

    JS执行过程 1.首先是预解析:预解析过程最重要的是提升,在JavaScript代码在预解析阶段,会对以var声明的变量名,和function开头的语句块,进行提升操作 2.执行操作 全局中解析和执行 ...

  5. html base 又一重大发现

    base 一个曾经不记得的标签,虽然接触Javaweb这么久了,但是还有很多基础性的东西都被我忽略掉了,还有很多基础但实用的技巧应该没有被我发现,虽然不使用这些技巧对功能实现没有多大影响.但是,发现这 ...

  6. (转)Struts2的标签库

    http://blog.csdn.net/yerenyuan_pku/article/details/68638679 Struts2的标签库 对于一个MVC框架而言,重点是实现两部分:业务逻辑控制器 ...

  7. HDU114 - Piggy-Bank 【完全背包】

    在 ACM 能够开展之前,必须准备预算,并获得必要的财力支持.该活动的主要收入来自于 Irreversibly Bound Money (IBM).思路很简单.任何时候,某位 ACM 会员有少量的钱时 ...

  8. Python 字符串和数字

    Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...

  9. odoo widget 标签介绍

    widget="statusbar" 头部状态条标签 widget="email" 电子邮件地址标签 widget="selection" ...

  10. asp.net 跨域问题

    asp.net 跨域问题 解决方案1: public void ProcessRequest(HttpContext context) { //解决跨域问题 context.Response.Clea ...