CodeForces 486C Palindrome Transformation 贪心+抽象问题本质

时间:2023-03-09 21:01:35
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 ;
}