codeforces 486-C. Palindrome Transformation(贪心)
2017-07-13 13:39
435 查看
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.
Examples
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的字符串,只由小写字母组成。现在在位置P处有一个光标,光标有左右移动的操作,也有上下改变字符的操作。请输出最少操作数使得这个串为回文串。
解题思路:
贪心。如果需要改动的字符超过两个,那么需要分两种情况来处理:
1)如果p在需要改动的字符之间,那么先选择最近的一边,然后迂回,去远的一边。
2)否则直接一条路走过去即可
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.
Examples
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的字符串,只由小写字母组成。现在在位置P处有一个光标,光标有左右移动的操作,也有上下改变字符的操作。请输出最少操作数使得这个串为回文串。
解题思路:
贪心。如果需要改动的字符超过两个,那么需要分两种情况来处理:
1)如果p在需要改动的字符之间,那么先选择最近的一边,然后迂回,去远的一边。
2)否则直接一条路走过去即可
#include<iostream> #include<cmath> using namespace std; #define maxn 1000005 char s[maxn]; int a[maxn]; int main() { int n,p; while(cin>>n>>p>>s) { int cnt=0,ans=0; for(int i=0;i<n/2;i++) if(s[i]!=s[n-i-1]) { a[cnt++]=i; ans+=abs(s[i]-s[n-i-1])>12?26-abs(s[i]-s[n-i-1]):abs(s[i]-s[n-i-1]); } p=p>n/2?n-p:p-1; ans+=cnt==0?0:cnt==1?abs(a[0]-p):(p>=a[0]&&p<=a[cnt-1])?min(p-a[0],a[cnt-1]-p)+a[cnt-1]-a[0]:max(abs(a[0]-p),abs(a[cnt-1]-p)); cout<<ans<<endl; } return 0; }
相关文章推荐
- codeforces-486【C-贪心-思维】【D-树状DP】
- codeforces 222D Olympiad(贪心)
- Codeforces 337C Quiz【贪心+快速幂】
- codeforces 700B Connecting Universities 贪心dfs
- Codeforces 355C Vasya and Robot【贪心+前缀和】
- CodeForces - 867E Buy Low Sell High 贪心 + 优先队列
- CodeForces 540B School Marks (贪心)
- CodeForces 545C Woodcutters (贪心orDP)
- 【CodeForces】699C - Vacations(贪心)
- Codeforces_732D_(二分贪心)
- [Codeforces 940.E] Cashback(dp,数据结构,贪心)
- CodeForces-729C-Road to Cinema(二分查找 贪心 排序)
- Codeforces 797C Minimal string【贪心】
- Codeforces - 720A. Closing ceremony - 贪心
- Codeforces 18D Seller Bob java大数+贪心
- codeforces 798 D. Mike and distribution 二维贪心
- CodeForces - 734C Anton and Making Potions 二分+贪心 思维题
- Codeforces 534B Covered Path【有点奇怪的思维+贪心】
- CodeForces 645 E.Intellectual Inquiry(dp+贪心)
- CodeForces - 609A USB Flash Drives ( 排序 简单贪心 )