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 ...
随机推荐
- openGL 旋转的图形 矩阵操作
#include <windows.h> #ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut. ...
- poj 1753 Flip Game 高斯消元
题目链接 4*4的格子, 初始为0或1, 每次翻转一个会使它四周的也翻转, 求翻转成全0或全1最少的步数. #include <iostream> #include <vector& ...
- How to configure connectingstrings in Web.config
先来看一下默认的连接SQL Server数据库配置<connectionStrings> <add name="LocalSqlServer" connect ...
- Matplotlib中文乱码
想要分析一批数据,画出图形会比较直观.所以就搜索了一下各种软件,最终选择使用python的matplotlib.原因也是因为python使用起来比较方便,虽然R才是分析数据的首选,不过,没有R的基础, ...
- 静态方法中为什么不能使用this
- DataTabe对象的例子
前: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataTable对象 ...
- android 播放assets文件里视频文件的问题
今天做了一个功能,就是播放项目工程里面的视频文件,不是播放SD卡视频文件. 因为之前写webview加载assets文件夹时,是这样写的: webView = new WebView(this); w ...
- HDU 1286 找新朋友
题解:分析题目,就是一个裸的欧拉函数,于是AC. #include <cstdio> int eular(int n){ int ret=1,i; for(i=2;i*i<=n;i+ ...
- The Longest Straight(二分,离散化)
Problem 2216 The Longest Straight Accept: 7 Submit: 14 Time Limit: 1000 mSec Memory Limit : 3 ...
- Unity5.0 RPG角色扮演历险类游戏之 森林历险记
http://v.youku.com/v_show/id_XMTI1MjEyNjc4MA==.html? from=y1.7-1.2