Codeforces 486C Palindrome Transformation(贪心)
题目链接:Codeforces 486C Palindrome Transformation
题目大意:给定一个字符串,长度N。指针位置P,问说最少花多少步将字符串变成回文串。
解题思路:事实上仅仅要是对称位置不同样的。那么指针肯定要先移动到这里,改动字符仅仅须要考虑两种方向哪种更优即
可。
然后将全部须要到达的位置跳出来。贪心处理。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 5;
int N, P;
vector<int> pos;
char s[maxn];
int solve () {
int ret = 0, n = N / 2;;
for (int i = 0; i < n; i++) {
int tmp = abs(s[i] - s[N-i-1]);
tmp = min(tmp, 26 - tmp);
ret += tmp;
if (tmp)
pos.push_back(abs(i+1-P) < abs(N-i-P) ? i+1 : N-i);
}
n = pos.size();
if (n == 0)
return ret;
sort(pos.begin(), pos.end());
return ret + pos[n-1] - pos[0] + min(abs(pos[n-1]-P), abs(pos[0]-P));
}
int main () {
scanf("%d%d%s", &N, &P, s);
printf("%d\n", solve());
return 0;
}
Codeforces 486C Palindrome Transformation(贪心)的更多相关文章
- codeforces 486C Palindrome Transformation 贪心求构造回文
点击打开链接 C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes ...
- CodeForces 486C Palindrome Transformation 贪心+抽象问题本质
题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...
- Codeforces Round 486C - Palindrome Transformation 贪心
C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...
- codeforces 486C. Palindrome Transformation 解题报告
题目链接:http://codeforces.com/problemset/problem/486/C 题目意思:给出一个含有 n 个小写字母的字符串 s 和指针初始化的位置(指向s的某个字符).可以 ...
- 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)---C. Palindrome Transformation (贪心)
Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input sta ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- cf486C Palindrome Transformation
C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...
随机推荐
- 分别给出BOOL,int,float,指针变量 与“零值”比较的 if 语句(假设变量名为var)
BOOL型变量:if(!var) int型变量: if(var==0) float型变量: const float EPSINON = 0.00001; if ((x > ...
- js 浏览器版本检测
整理了一下浏览器检测的js脚本 分享给大家 浏览器检测一般都是在网页打开的时候执行 使用js的闭包来实现页面加载以后执行的脚本 (function(){ //页面加载后执行的脚本 })() ; 检测浏 ...
- C# 解析User-Agent工具
分享一个解析User-Agent的程序集: Neget程序集名称:UAParser Github下载地址:https://github.com/qiailu/uap-csharp 扩展程序集:UAPa ...
- 准备开发一个基于canvas的图表库,记录一些东西(一)
开源的图表库已经有很多了,这里从头写个自己的,主要还是 提高自己js的水平,增加复杂代码组织的经验 首先写一个画图的库,供以后画图表使用.经过2天的开发,算是能拿出点东西了,虽然功能还很弱,但是有了一 ...
- linux磁盘空间用满的处理方法
linux下空间满可能有两种情况 可以通过命令 df -h 查看磁盘空间占用,实际上是查看磁盘块占用的文件(block) df -i 查看索引节点的占用(Inodes) 磁盘块和索引节点其中之一满 ...
- RequireJS学习笔记(转)
前言 进入移动前端是很不错的选择,这块也是我希望的道路,但是不熟悉啊... 现在项目用的是require+backbone,整个框架被封装了一次,今天看了代码搞不清楚,觉得应该先从源头抓起,所以再看看 ...
- Java中解析XML的四种方法
XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这里面包括的内容有DOM ...
- d007: 求两数的整数商 和 余数
内容: 求两数的整数商 和 余数 输入说明: 一行两个整数 输出说明: 一行两个整数 输入样例: 18 4 输出样例 : 4 2 #include <stdio.h> int main ...
- 从客户端(xxxxxxxxxxxxxxxxxxxxxx)中检测到有潜在危险的 Request.Form 值。
在项目中用到了富文本编辑器,当将编辑器中的值从视图传递到控制器时,控制器就会向浏览器返回“从客户端(xxxxxxxxxxxxxxxxxxxxxx)中检测到有潜在危险的 Request.Form 值.” ...
- 开发H5小游戏
Egret白鹭H5小游戏开发入门(一) 前言: 好久没更新博客了,以前很多都不会,所以常常写博客总结,倒是现在有点点经验了就懒了.在过去的几个月里,在canvas游戏框架方面,撸过了CreateJ ...