Codeforces 486C palindrome Transformation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.