「CF486C」Palindrome Transformation
题解
显然代价是对称的,仅需要考虑改半边
然后初始点两边都需要更改,要考虑是先往左还是往右
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<algorithm> #include<queue> #include<cmath> #include<map> #include<queue> #define inf 2000000000 #define ll long long using namespace std; 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; } int l=inf,r; int n,m,ans; char ch[100005]; int a[100005]; int main() { n=read();m=read(); scanf("%s",ch+1); if(m>n/2)m=n-m+1; for(int i=1;i<=n/2;i++) { a[i]=(int)abs(ch[n-i+1]-ch[i]); a[i]=min(a[i],26-a[i]); ans+=a[i]; } for(int i=1;i<=n/2;i++) if(a[i]) l=min(l,i),r=max(r,i); if(!ans)puts("0"); else printf("%d\n",ans+r-l+min(abs(m-l),abs(r-m))); return 0; } |
Subscribe