码迷,mamicode.com
首页 > 其他好文 > 详细

贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

时间:2015-08-03 14:27:13      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 1 /*
 2     贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了
 3 */
 4 /************************************************
 5 Author        :Running_Time
 6 Created Time  :2015-8-3 9:14:02
 7 File Name     :B.cpp
 8  *************************************************/
 9 
10 #include <cstdio>
11 #include <algorithm>
12 #include <iostream>
13 #include <sstream>
14 #include <cstring>
15 #include <cmath>
16 #include <string>
17 #include <vector>
18 #include <queue>
19 #include <deque>
20 #include <stack>
21 #include <list>
22 #include <map>
23 #include <set>
24 #include <bitset>
25 #include <cstdlib>
26 #include <ctime>
27 using namespace std;
28 
29 #define lson l, mid, rt << 1
30 #define rson mid + 1, r, rt << 1 | 1
31 typedef long long ll;
32 const int MAXN = 1e5 + 10;
33 const int INF = 0x3f3f3f3f;
34 const int MOD = 1e9 + 7;
35 char s[MAXN];
36 int a[MAXN];
37 int n, p;
38 
39 int main(void)    {       //Codeforces Round #277 (Div. 2) C. Palindrome Transformation
40     scanf ("%d%d", &n, &p); scanf ("%s", s + 1);    
41     if (p > n / 2)  p = n - p + 1;
42     int l = n + 1, r = 0;   int ans = 0;
43     for (int i=1; i<=n/2; ++i)    {
44         if (s[i] != s[n-i+1]) {
45             l = min (l, i); r = max (r, i);
46             ans += min (abs (s[i] - s[n-i+1]), 26 - abs (s[i] - s[n-i+1]));
47         }
48     }
49     if (ans == 0)   puts ("0");
50     else    {
51         printf ("%d\n", ans + r - l + min (abs (p - l), abs (r - p)));
52     }
53 
54     return 0;
55 }

下面的图片更形象点。。。

技术分享

贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4699101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!