Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
1 second
256 megabytes
standard input
standard 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?
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.
Print the minimum number of presses needed to change string into a palindrome.
8 3
aeabcaez
6
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.
解题思路:贪心。仅仅考虑一边的情况就可以,以左半为例。不断贪心考虑pos离哪边的不匹配边界近,如此往复就可以。
AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath> using namespace std; int main(){
int n, pos;
string s;
// freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &pos)!=EOF){
pos --;
cin>>s;
int l = 0, r = n/2 - 1; if(n/2 <= pos) pos = n - 1 - pos; while(l < n/2 && s[l] == s[n - 1 - l]) l++;
while(r >= 0 && s[r] == s[n - 1 - r]) r--; int ans = 0; for(int i=l; i<=r; i++){
int temp = abs(s[i] - s[n-1-i]);
ans += min(temp, 26 - temp);
}
if(l < r)
ans += min(abs(pos - l), abs(r - pos)) + r - l;
else if(l == r)
ans += abs(pos - r);
cout<<ans<<endl;
}
return 0;
}
Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)的更多相关文章
- Codeforces Round #277 (Div. 2)C.Palindrome Transformation  贪心
		
C. Palindrome Transformation Nam is playing with a string on his computer. The string consists o ...
 - 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation
		
题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...
 - Codeforces Round #277 (Div. 2) 题解
		
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
 - 【codeforces】Codeforces Round #277 (Div. 2) 解读
		
门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...
 - Codeforces Round #277(Div 2) A、B、C、D、E题解
		
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. Calculating Function 水题,判个奇偶即可 #includ ...
 - Codeforces Round #277 (Div. 2)  解题报告
		
题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...
 - 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 ...
 - 套题 Codeforces Round #277 (Div. 2)
		
A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...
 - 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. ...
 
随机推荐
- 解决for循环下变量显示一致的问题
			
for(var i=0;i<10;i++){ setTimeOut(function(){ console.log("i:",i); },100) } 上面显示的打印出来结果 ...
 - VIO第二讲_allen方差工具
			
1,首先,安装ceres依赖项,见高博14讲116页,然后下载编译安装ceres: git clone https://github.com/ceres-solver/ceres-solver cd ...
 - Java之字符,字符串替换
			
/** 4. 字符串的替换操作 1. String replace(char oldChar,char newChar) //将新字符替换旧字符 3. String replaceFirst(Stri ...
 - 第1节 flume:8、flume采集某个文件内容到hdfs上
			
2. 采集文件内容到HDFS 需求分析: 采集需求:比如业务系统使用log4j生成的日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs. 同一个日志文件的内容不断增 ...
 - [BZOJ3207]:花神的嘲讽(分块解法)
			
题目传送门 题目描述:背景花神是神,一大癖好就是嘲讽大J,举例如下:“哎你傻不傻的![hqz:大笨J]”“这道题又被J屎过了!!”“J这程序怎么跑这么快!J要逆袭了!”…… 描述这一天DJ在给吾等众蒟 ...
 - Linux离线安装redis集群
			
一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,联网环境安装较为简单,这里只说脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网,服务 ...
 - 【struts2】学习笔记
			
常见问题及注意事项: 1.下载struts2时,要看清所下载的版本,不同版本web.xml配置路径不同! 2. 导入jar包时,导入的包要完全准确,缺少或过多的会导致缺失或冲突! 3. Registe ...
 - Backspace doesn't delete inner html tags of a contenteditable DIV in Firefox
			
https://bugzilla.mozilla.org/show_bug.cgi?id=439808 backspace键 在ff下不能使用 div contenteditable=true时
 - @locked_cached_property   ---flask.helpers模块
			
源码: class locked_cached_property(object): """A decorator that converts a function int ...
 - HDU2069-Coin Change
			
Coin Change 这题题意和UVA674很像,但加了一个限制条件不能超过100个硬币.于是我们可以用d[i][j]来表示硬币数量为i时金钱总数为j时的方法总数,总钱不能超过250. const ...