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
output
Note
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is:
代码
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; }
代码
相关文章推荐
- Wireshark网络分析就这么简单 -- 目录
- linux 常用基础命令 cat 详细介绍
- Day Two
- ISO/IEC 9899:2011 条款6.7.6——声明符
- Linux 技巧:让进程在后台可靠运行的几种方法
- 又看自己年少时的代码
- Day 5(11.8):(5)讲义
- jquery中attr和prop的区别分析
- ExtJS5获取当前光标位置插入文本内容
- redis并发问题
- ubuntu下服务自启与关闭
- 安卓网络编程1--ANR异常和Handler机制
- HTML中get 和post 的区别(转)
- [Unity热更新]lua基础(一)
- AJAX在jQuery中使用步骤
- Codeforces Round #277 (Div. 2) B.OR in Matrix 模拟
- Activity的窗口对象(Window)的创建过程分析
- Day 5(11.8):(4)多表连接
- Android 源码分析工具
- 在Java中调用Oracle存储过程的总结