您的位置:首页 > 其它

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

2015-11-08 23:46 417 查看
[b]C. Palindrome Transformation[/b]

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:

///1085422276
#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=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;
}
//****************************************
#define maxn 100000+50
#define inf 1000000007

int main(){
int n,k;
char a[maxn];
int G[maxn];
bool bo=0;
scanf("%d%d",&n,&k);getchar();
for(int i=1;i<=n;i++){
scanf("%c",&a[i]);
}
int l=1,r=n,pos,next[maxn];
if(k<=n/2)pos=0;
else pos=1;
int kk=0;G[0]=-inf;
while(l<=r){
if(a[l]!=a[r]){
if(pos==1){
G[++kk]=r;
if(r==k)bo=1;
next[r]=l;
}
else{
if(l==k)bo=1;G[++kk]=l;next[l]=r;
}
}

l++,r--;
}
if(!bo){
G[++kk]=k;
}
sort(G+1,G+kk+1);
int ans=0;
int last;//cout<<kk<<endl;
int fa=lower_bound(G+1,G+kk+1,k)-G;
if(abs(G[1]-G[fa])>=abs(G[kk]-G[fa])){
last=G[fa];//cout<<last<<" "<<G[fa]<<endl;
for(int i=fa+1;i<=kk;i++){
ans+=min(26-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-1;i>=1;i--){
ans+=min(26-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-1;i>=1;i--){
ans+=min(26-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[1];
for(int i=fa+1;i<=kk;i++){
ans+=min(26-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(26-abs(a[k]-a[next[k]]),abs(a[next[k]]-a[k]));
cout<<ans<<endl;

return 0;
}


代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: