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. ...
随机推荐
- 【MySQL】二进制分发安装
操作系统:Red Hat Enterprise Linux Server release 6.5 Mysql安装包:mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz ...
- JS高级——封装注册事件
兼容性问题 1.ele.on事件类型 = function(){}一个元素ele注册一种事件多次,会被替换成最后一个,所以有局限性 2.addEventListener(事件类型,事件处理函数,use ...
- html5——伸缩布局
基本概念 1.主轴:Flex容器的主轴主要用来配置Flex项目,默认是水平方向 2.侧轴:与主轴垂直的轴称作侧轴,默认是垂直方向的 3.方向:默认主轴从左向右,侧轴默认从上到下 4.主轴和侧轴并不是固 ...
- JS——祝愿墙
注意事项: 1.for循环的下一层注册了事件的话,事件函数中关于变量i的节点元素是不允许出现的,因为在函数加载的时候,只会加载函数名,不会加载函数体,外层for循环会走完一边,变量i一直会停留在最后一 ...
- SQL基本操作——DROP撤销索引、表以及数据库
DROP撤销索引.表以及数据库 --DROP INDEX 命令删除表格中的索引 DROP INDEX table_name.index_name --DROP TABLE 语句删除表(表的结构.属性以 ...
- if({1,0}, , )
=VLOOKUP(F2,IF({1,0},D2:D10,C2:C10),2,),用F2作为查找条件,对D列进行查找,如果改成{0,1}则是对C列为查找范围.返回内存数组,对F2进行精确查找. 为什么写 ...
- 字符串问题:去掉字符串中连续出现 k 个 0 的子串
[题目] 给定一个字符串 str 和 一个整数 k, 如果 str 中正好有连续 k 个 ‘0’ 字符出现时,把 k 个连续的 ‘0’ 字符去除,返回处理后的字符串. [举例] str="A ...
- Asp.Mvc 常用
url转义 var address = "http://www.cnblog.com"; var a22 = Uri.EscapeDataString(address); var ...
- Linux - redis哨兵集群实例
目录 Linux - redis哨兵集群实例 命令整理 配置流程 Linux - redis哨兵集群实例 命令整理 官网地址:http://redisdoc.com/ redis-cli info # ...
- hdu 5174 Ferries Wheel
摩天轮是一个环,周围围绕着一些缆车.每个缆车按顺序编号为1,2,3...K-1,K1,2,3...K−1,K而且每个缆车也拥有一个唯一的值且保证A[i-1] < A[i] < A[i+1] ...