Codeforces Round #277 (Div. 2) C
来源:互联网 发布:mysql触发器 编辑:程序博客网 时间:2024/05/17 00:17
C. Palindrome Transformation
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
#include <bits/stdc++.h>using namespace std;char str[123456];char ch1[123456];char ch2[123456];int foo[123456];int main() { int n,k; scanf("%d%d",&n,&k); scanf("%s",str); int h=0; int s=0; int sum=0,l=0; for(int i=0; i<=n/2-1; i++) ch1[h++]=str[i]; for(int i=n-1; i>=n/2; i--) ch2[s++]=str[i]; for(int i=0; i<h; i++) { if(ch1[i]!=ch2[i]) { foo[l]=i; foo[l]++; l++; sum+=min(abs(ch1[i]-ch2[i]),26-abs(ch1[i]-ch2[i])); } } if(k>h) k=n-k+1; if(l==0) { printf("%d\n",sum); } else if(l==1) { printf("%d\n",sum+abs(foo[0]-k)); } else { printf("%d\n",sum+abs(foo[0]-foo[l-1])+min(abs(foo[0]-k),abs(foo[l-1]-k))); } return 0;}
- Codeforces Round #277 (Div. 2) C
- 【codeforces】Codeforces Round #370 (Div. 2) C
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277(Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2) 解题报告(B C)
- Codeforces Round #277 (Div. 2)C——Palindrome Transformation
- Codeforces Round #277 (Div. 2) A,B,C,D,E
- Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
- Codeforces Round #277 (Div. 2) C. Palindrome Transformations 贪心
- Codeforces Round #277 (Div. 2) A·B·C
- Codeforces Round #105 (Div. 2) C
- Codeforces Round 134 div 2 C题
- C语言可变长参数列表原理与实现
- Unity性能优化
- struts2--1.入门程序
- spark算子join讲解
- 求N范围内的所有素数
- Codeforces Round #277 (Div. 2) C
- 谈谈小白眼里的竞品分析究竟为什么要这么做
- 继承—子父类中的构造函数-子类的实例化过程
- 我的Java之旅 第八课 Servlet 进阶API、过滤器与监听器
- 博为峰Java技术文章 ——JavaSE Swing使用ComboBoxModel创建下拉列表框Ⅰ
- Android Studio如何创建VR项目。
- 让listview、gridview 回到第一行
- ubuntu上识别华为4G上网卡(随行wifi)USB模式
- 发布自己的项目到github及其遇到问题