zoukankan      html  css  js  c++  java
  • Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心

    C. Palindrome Transformation
     
     

    Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.

    There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).

    When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.

    Initially, the text cursor is at position p.

    Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?

    Input

    The first line contains two space-separated integers n (1 ≤ n ≤ 105) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.

    The next line contains n lowercase characters of Nam's string.

    Output

    Print the minimum number of presses needed to change string into a palindrome.

    Sample test(s)
    input
    8 3
    aeabcaez
    output
    6
    Note

    A string is a palindrome if it reads the same forward or reversed.

    In the sample test, initial Nam's string is:  (cursor position is shown bold).

    In optimal solution, Nam may do 6 following steps:

    The result, , is now a palindrome.

    题意:给你一个长度n的字符串和光标的起始位置,

             再给出以下4种操作 光标左移 光标右移 字符上换 字符下换。 问将给出的字符换成回文串的最小花费是多少。

    题解:可以先预处理出每个地方的光标上下变换次数,再贪心求步数最小

            我们当然贪心在一半边内移动

    ///1085422276
    #include<bits/stdc++.h>
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    #define meminf(a) memset(a,127,sizeof(a));
    
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){
            if(ch=='-')f=-1;ch=getchar();
        }
        while(ch>='0'&&ch<='9'){
            x=x*10+ch-'0';ch=getchar();
        }return x*f;
    }
    //****************************************
    #define maxn 100000+50
    #define inf 1000000007
    
    int main(){
    int n,k;
    char a[maxn];
    int G[maxn];
      bool bo=0;
       scanf("%d%d",&n,&k);getchar();
       for(int i=1;i<=n;i++){
          scanf("%c",&a[i]);
       }
       int l=1,r=n,pos,next[maxn];
       if(k<=n/2)pos=0;
       else pos=1;
       int kk=0;G[0]=-inf;
       while(l<=r){
         if(a[l]!=a[r]){
              if(pos==1){
                G[++kk]=r;
                if(r==k)bo=1;
                next[r]=l;
              }
              else{
                  if(l==k)bo=1;G[++kk]=l;next[l]=r;
              }
         }
    
         l++,r--;
       }
       if(!bo){
          G[++kk]=k;
       }
       sort(G+1,G+kk+1);
       int ans=0;
       int last;//cout<<kk<<endl;
       int fa=lower_bound(G+1,G+kk+1,k)-G;
       if(abs(G[1]-G[fa])>=abs(G[kk]-G[fa])){
            last=G[fa];//cout<<last<<" "<<G[fa]<<endl;
         for(int i=fa+1;i<=kk;i++){
            ans+=min(26-abs(a[G[i]]-a[next[G[i]]]),abs(a[G[i]]-a[next[G[i]]]));;
            ans+=abs(G[i]-last);last=G[i];
         }last=G[kk];//cout<<ans<<endl;
         for(int i=fa-1;i>=1;i--){
            ans+=min(26-abs(a[G[i]]-a[next[G[i]]]),abs(a[G[i]]-a[next[G[i]]]));
           ans+=abs(G[i]-last);last=G[i];
         }//cout<<ans<<endl;
       }
       else {
        last=G[fa];
         for(int i=fa-1;i>=1;i--){
            ans+=min(26-abs(a[G[i]]-a[next[G[i]]]),abs(a[G[i]]-a[next[G[i]]]));
            ans+=abs(G[i]-last);;last=G[i];
         } last=G[1];
         for(int i=fa+1;i<=kk;i++){
            ans+=min(26-abs(a[G[i]]-a[next[G[i]]]),abs(a[G[i]]-a[next[G[i]]]));
            ans+=abs(G[i]-last);;last=G[i];
         }
       }if(bo)ans+=min(26-abs(a[k]-a[next[k]]),abs(a[next[k]]-a[k]));
       cout<<ans<<endl;
    
      return 0;
    }
    代码
  • 相关阅读:
    0326系统按钮添加权限的工作(jsp权限写法)
    0321菜单数据
    ztree树的递归
    前端padding margin
    解决1像素边框问题
    水平垂直居中
    移动端css公共样式
    tap方法改良this指向
    移动端常见问题
    前端笔记(兼容)
  • 原文地址:https://www.cnblogs.com/zxhl/p/4948742.html
Copyright © 2011-2022 走看看