Title: Poke Me
Test instructions: Given a string of length n, given the initial cursor position p, supports 4 kinds of operations, Left,right moves the cursor point, up,down, changes the current cursor point to the character, the output of the least action makes the string palindrome.
Analysis: Only focus on string N/2 length, up,down operation is fixed, that is, can not be optimized, the remaining is the left,down operand, fine think the middle without tube, only focus on the first from left to the middle of the position to change and the last to change the position can be, specific look at the code.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intM = 1e5+5;7 8 intn, p;9 CharStr[m];Ten intMain () One { A while(~SCANF ("%d%d", &n, &p)) { - GetChar (); -Gets (str+1 ); the intSumchg =0; Total number of Up,down operations - intFirst =0; The first place to change - BOOLFIRSTJD =true; - intLast =0; The last place to change + for(intI=1; i<=n/2; i++ ) { - intD = ABS (str[i]-str[n+1-i]); + if(d) { A if(FIRSTJD) { atFirst =i; -FIRSTJD =false; - } -Last =i; -Sumchg + = min (d, --d); Select the minimum number of actions for up or down - } in } - if(P > n/2)//due to the palindrome symmetry, so p in the middle right can also be p when the left symmetrical position calculation top = n+1-p; + intRET =0; - if(Sumchg = =0) {//Do not need to change output 0 theprintf"%d\n", ret); * Continue; $ }Panax Notoginseng if(First >=P )//If P is on the first left to change, p can only go right, i.e. -RET + = Sumchg + Last-p; the Else if(Last <=P )//If P is on the last to change to the right, p can only go left, that is, to perform a leave operation +RET + = Sumchg + P-First ; A Else theret + = min (2* (P-first) +last-p,2* (last-p) +p-first) +Sumchg; P in the middle, take the minimum value to the left and right. +printf"%d\n", ret); - } $ return 0; $}
Codeforces 486C palindrome Transformation