• 付忠庆的练习小笔记-Codeforces #277 Div2 C


    原题链接

    http://codeforces.com/contest/486/problem/C

    这个C题显然更水一些

    步数可以分为两种 上下一种 左右一种

    总步数最小 = 上下最小+左右最小

    先讨论上下最小 就是从两个方向去由字母1到字母2  min(dis(A,B),26-dis(A,B));

    然后讨论左右最小 pos是起始指针的位置

    1-统一在前半段操作,pos要是在后半段就“映射”到前半段,pos=n+1-pos;

    2-若上下操作为0 那么左右直接也是0

    3-找出左边第一个操作l 右边第一个操作r

          一, 若是 pos<l<r   返回 r-pos

      二,  若是 l<r<pos 返回 pos-l

      三,若是 l<pos<r  返回 min(r-pos,pos-l);

    结束

    附代码:

     1 #include <iostream>
     2 #include <cmath>
     3 typedef long long LL;
     4 using namespace std;
     5 int a[110000],f[100000];
     6 
     7 int main()
     8 {
     9     int n,pos;
    10     while (cin>>n>>pos)
    11     {
    12         if (pos>n/2) pos=n+1-pos;
    13         LL ans=0;
    14         char temp;
    15         for (int i=1;i<=n;i++)
    16         {
    17             cin>>temp;
    18             a[i]=temp-'a'+1;
    19         }
    20         for (int i=1;i<=n/2;i++)
    21         {
    22             int dis = abs(a[i]-a[n-i+1]);
    23             f[i]=min(dis,26-dis);
    24             ans+=f[i];
    25         }
    26         if (ans==0) {cout<<0<<endl;continue;}
    27         int l=1,r=n/2;
    28         while(!f[l++]&&l<=r);
    29         l--;
    30         while(!f[r--]&&l<=r);
    31         r++;
    32         if (pos>r) ans+=pos-l;
    33         if (pos<l) ans+=r-pos;
    34         if (pos<=r&&pos>=l) ans+=(r-l)+min(r-pos,pos-l);
    35         cout<<ans<<endl;
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    【强行点出机械师天赋,修复无脸男储钱罐】
    【自由随想录(二)】
    【自由随想录(一)】
    iOS Alamofire插件使用方法
    iOS获取设备ip地址(OC版)
    获取位置
    objective-c 开发最简单的UITableView时数据进不去的问题
    java 获取真实IP
    数据库添加外键
    mysql 清库
  • 原文地址:https://www.cnblogs.com/fuzhongqing/p/4093570.html
Copyright © 2020-2023  润新知