Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心
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.
题意:给你一个长度n的字符串和光标的起始位置,
再给出以下4种操作 光标左移 光标右移 字符上换 字符下换。 问将给出的字符换成回文串的最小花费是多少。
题解:可以先预处理出每个地方的光标上下变换次数,再贪心求步数最小
我们当然贪心在一半边内移动
///
#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=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';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=;
scanf("%d%d",&n,&k);getchar();
for(int i=;i<=n;i++){
scanf("%c",&a[i]);
}
int l=,r=n,pos,next[maxn];
if(k<=n/)pos=;
else pos=;
int kk=;G[]=-inf;
while(l<=r){
if(a[l]!=a[r]){
if(pos==){
G[++kk]=r;
if(r==k)bo=;
next[r]=l;
}
else{
if(l==k)bo=;G[++kk]=l;next[l]=r;
}
} l++,r--;
}
if(!bo){
G[++kk]=k;
}
sort(G+,G+kk+);
int ans=;
int last;//cout<<kk<<endl;
int fa=lower_bound(G+,G+kk+,k)-G;
if(abs(G[]-G[fa])>=abs(G[kk]-G[fa])){
last=G[fa];//cout<<last<<" "<<G[fa]<<endl;
for(int i=fa+;i<=kk;i++){
ans+=min(-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-;i>=;i--){
ans+=min(-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-;i>=;i--){
ans+=min(-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[];
for(int i=fa+;i<=kk;i++){
ans+=min(-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(-abs(a[k]-a[next[k]]),abs(a[next[k]]-a[k]));
cout<<ans<<endl; return ;
}
代码
Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心的更多相关文章
- 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation
题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...
- Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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. ...
随机推荐
- 【转】 Java 集合系列07之 Stack详细介绍(源码解析)和使用示例
概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...
- 【1】Jdk1.8中的HashMap实现原理
HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变. 内部实现 ...
- [ AHOI 2008 ] Meet
\(\\\) \(Description\) 一棵\(N\)个节点的树,每条边权都为\(1\). \(M\)组询问,每次给出三个点\(A_i,B_i,C_i\),求从三个点分别出发,移动到同一个点的路 ...
- node的api
一. 1.url: 绝对URI http://user:pass@www.example.com:80/dir/index.html?uid=1#ch1 协议 登录信息 服务器地址 端口 文件路径 查 ...
- jQuery——插件制作
1.$.fn.extend:扩展 jQuery 元素集来提供新的方法(通常用来制作插件),使用时是$('选择器').方法 2.$.extend:扩展jQuery对象本身,用来在jQuery命名空间上增 ...
- [Windows Server 2012] Filezilla安全加固方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:FileZ ...
- 12--C++_运算符重载
C++_运算符重载 什么是运算符的重载? 运算符与类结合,产生新的含义. 为什么要引入运算符重载? 作用:为了实现类的多态性(多态是指一个函数名有多种含义) 怎么实现运算符的重载? 方式:类的成员函数 ...
- privot函数使用
语法: table_source PIVOT( 聚合函数(value_column) FOR pivot_column IN(<column_list>) ) 将列转化为行 写个小示例 : ...
- Java中字符串的常用属性与方法
•字符串常用的属性 string.length()————>返回字符串的长度,int类型. •字符串常用的方法 String.contains(";")——————>判 ...
- SolidWorks学习笔记(一)
一.草图绘制 1.简单命令 先直线后圆弧,几何约束.尺寸标定 2.圆周阵列 3.几何关系——对称 添加几何约束:圆弧相等.关于竖直中心线的对称 4.捕捉圆心 5.尺寸锁定,有圆弧的,先直后圆 6. 7 ...