付忠庆的练习小笔记-Codeforces #277 Div2 C
生活随笔
收集整理的這篇文章主要介紹了
付忠庆的练习小笔记-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 }?
轉載于:https://www.cnblogs.com/fuzhongqing/p/4093570.html
總結
以上是生活随笔為你收集整理的付忠庆的练习小笔记-Codeforces #277 Div2 C的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3dmm人脸配准/重建:gold sta
- 下一篇: QImage与QPixmap区别