CodeForces 486C Palindrome Transformation 贪心+抽象问题本质
题目:戳我
题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文。
分析:只关注字符串n/2长度,up,down操作是固定不变的,也就是不能优化了,剩下就是left,down的操作数,细想下中间不用管,只关注从左到中间第一个要改变的位置和最后一个要改变的位置即可,具体看代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 1e5+; int n, p;
char str[M];
int main() {
while( ~scanf("%d %d", &n, &p ) ) {
getchar();
gets( str+ );
int sumchg = ; //up,down的操作总数
int first = ; //第一个要改变的位置
bool firstjd = true;
int last = ; //最后一个要改变的位置
for( int i=; i<=n/; i++ ) {
int d = abs( str[i]-str[n+-i] );
if( d ) {
if( firstjd ) {
first = i;
firstjd = false;
}
last = i;
sumchg += min( d, -d ); //选择up或者down的最小操作数
}
}
if( p > n/ ) //由于回文左右对称,所以p在中间右边时也可将p当左边对称位置计算
p = n+-p;
int ret = ;
if( sumchg == ) { //不需要改变输出0
printf("%d\n", ret);
continue;
}
if( first >= p ) //如果p在第一个要改变的左边,p只能向右走,即执行right操作
ret += sumchg + last - p;
else if( last <= p ) //如果p在最后一个要改变的右边,p只能向左走,即执行left操作
ret += sumchg + p - first;
else
ret += min( *(p-first)+last-p, *(last-p)+p-first ) + sumchg; //p在中间,取求向左向右走的最小值
printf("%d\n", ret);
}
return ;
}
CodeForces 486C Palindrome Transformation 贪心+抽象问题本质的更多相关文章
- Codeforces 486C Palindrome Transformation(贪心)
题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...
- codeforces 486C Palindrome Transformation 贪心求构造回文
点击打开链接 C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes ...
- 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 ...
随机推荐
- 【转】DevExpress控件安装
原文链接: DevExpress控件安装.汉化使用教程 - 田园里的蟋蟀 学习网址: 1.DevExpress控件中文网 2.DevExpress控件中文网使用教程 3.DevExpress控件使用经 ...
- java断言
public class New{ public static void main(String[] args){ assert false; System.out.println("pas ...
- 驯服你的Windows Server 2003
虽然通过一些技巧可以让Windows Server 2003更符合我们的使用习惯,但对我等菜鸟来说,操作还是有相当的难度,有没有更简单.更省事的驯服它的办法呢?有,那就是使用Windows Serve ...
- 宣布在日本地区正式发布 Windows Azure
昨天,我与 Microsoft 日本的集团副总裁 Yasuyuki Higuchi 一同站在台上,宣布在两个新地区正式发布 Windows Azure:日本东部和日本西部.能够亲自见证 Micr ...
- Android 关于调用系统内已安装的相机问题
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent,1); 调用系统内已安 ...
- 用git上传项目到github
1 git clone github仓库地址 2 git add . 3 git commit -m "changes log" 4 git remote add origi ...
- android TDD平台插入双卡时,查看允许返回发送报告的选项,去掉勾选,不起作用
请在MultiSimPreferenceActivity.java 下修改 修改1: 函数 isChecked() private boolean isChecked(String prefe ...
- android 关于LCD背光调节渐变过程引起背光闪烁问题
如果背光渐变过程会引起背光闪烁,可以采取以下任意一种方法修改: 方法1.减少调节级别时间 http://blog.csdn.net/sergeycao 默认的设计在关闭背光时会有灭屏动画,就是 ...
- Cocos2d-x 3.0 使用TinyXml 解析XML文件
在cocos2d-x 3.0中Xml解析已经不用自己找库了,已经为我们集成好了. text.xml <!--?xml version ="1.0" encoding =&qu ...
- innerText和innerHTML的区别
innerhtml用法 innertext用法 以及innerHTML与innertext的区别,看完这个大家以后在实际应用中,就可以选择合适的方法.尽可能的考虑到兼容性. test.innerHTM ...