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

题目传送门

 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 }

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

时间: 2024-08-06 07:29:10

贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation的相关文章

【codeforces】Codeforces Round #277 (Div. 2) 解读

门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; LL n ; int main () { while ( ~scanf ( "%I64d" , &n )

Codeforces Round #277 (Div. 2) d

/**  * @brief Codeforces Round #277 (Div. 2) d  * @file d.cpp  * @author 面码  * @created 2014/11/17 14:53  * @edited  2014/11/17 14:53  * @type dfs dp  *   *  */ #include <cstdio> #include <vector> #define MOD 1000000007 #define MAXN 2014 using

Codeforces Round #277 (Div. 2) c

/**  * @brief Codeforces Round #277 (Div. 2) c  * @file c.c  * @author 面码  * @created 2014/11/14 13:39  * @edited  2014/11/14 13:39  * @type greedy  *  */ #include <stdio.h> #define MAXN 100010 #define MAXC 26 #define max(a, b)  ((a) > (b) ? (a) 

Codeforces Round #277 (Div. 2) a

/**  * @brief Codeforces Round #277 (Div. 2) a  * @author xiyan  * @created 2014/11/13 11:23  * @edited  2014/11/13 11:24  * @type math   *   *  */ #include <stdio.h> long long int a; int main() {     scanf("%I64d", &a);     printf(&qu

Codeforces Round #277 (Div. 2) b

/**  * @brief Codeforces Round #277 (Div. 2) b  * @author 面码  * @created 2014/11/13 14:01  * @edited  2014/11/13 14:01  * @type greedy  * @TODO less space and time cost with bitmap   *   *  */ #include <stdio.h> #define MAXN 110 //int a[MAXN][MAXN];

构造 Codeforces Round #Pi (Div. 2) B. Berland National Library

题目传送门 1 /* 2 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 3 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况讨论一下 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-6 0:23:37 8 * File Name :B.cpp 9

贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet

题目传送门 1 /* 2 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 using namespace std; 8 9 const int MAXN = 11; 10 const int INF = 0x3f3f3f3f; 11 char s[MAXN][MAXN]; 12 1

暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

题目传送门 1 /* 2 题意:删除若干行,使得n行字符串成递增排序 3 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-3 10:49:53 8 File Name :C.cpp 9 ************************************

构造 Codeforces Round #107 (Div. 2) B. Phone Numbers

题目传送门 1 /* 2 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 #include <vector> 9 #include <map> 10 #include <iostream> 11 #include &