贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation
/*
贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了
*/
/************************************************
Author :Running_Time
Created Time :2015-8-3 9:14:02
File Name :B.cpp
*************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
char s[MAXN];
int a[MAXN];
int n, p; int main(void) { //Codeforces Round #277 (Div. 2) C. Palindrome Transformation
scanf ("%d%d", &n, &p); scanf ("%s", s + );
if (p > n / ) p = n - p + ;
int l = n + , r = ; int ans = ;
for (int i=; i<=n/; ++i) {
if (s[i] != s[n-i+]) {
l = min (l, i); r = max (r, i);
ans += min (abs (s[i] - s[n-i+]), - abs (s[i] - s[n-i+]));
}
}
if (ans == ) puts ("");
else {
printf ("%d\n", ans + r - l + min (abs (p - l), abs (r - p)));
} return ;
}
下面的图片更形象点。。。

贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation的更多相关文章
- Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #277 (Div. 2)C.Palindrome Transformation 贪心
C. Palindrome Transformation Nam is playing with a string on his computer. The string consists o ...
- 【codeforces】Codeforces Round #277 (Div. 2) 解读
门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...
- Codeforces Round #277 (Div. 2) 题解
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 构造 Codeforces Round #302 (Div. 2) B Sea and Islands
题目传送门 /* 题意:在n^n的海洋里是否有k块陆地 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 输出完k个L后,之后全部输出S:) 5 10 的例子可以是这样的: LSLS ...
- 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet
题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...
- 构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers
题目传送门 /* 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 构造:先求出使第1个指向0要多少 ...
- 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library
题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...
随机推荐
- openjudge7624 山区建小学
描述 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄只能通过这条路来往.已知任意两个相邻的村庄之间的距离为di(为正整数),其中,0 < i < ...
- codevs——1081 线段树练习 2
1081 线段树练习 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description 给你N个数,有两种操作 1:给 ...
- JDBC的流数据
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/streaming-data.html: PreparedStatement对象必须具备使用输入和输出流 ...
- Servlet表单数据处理
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/form-data.html: 当需要从浏览器到Web服务器传递一些信息并最终传回到后台程序时,一 ...
- searchIndexer.exe占用过高CPU
1.searchIndexer.exe占用过高CPU很可能是Windows的索引损坏造成的. 这时候只要进入控制面板—索引选项—高级—重建即可解决 2. net stop "windows ...
- Mysql中错误日志、binlog日志、查询日志、慢查询日志简单介绍
前言 数据库的日志是帮助数据库管理员,追踪分析数据库以前发生的各种事件的有力根据.mysql中提供了错误日志.binlog日志(二进制日志).查处日志.慢查询日志.在此,我力求解决下面问题:各个日志的 ...
- A* Pathfinding Project (Unity A*寻路插件) 使用教程
Unity4.6 兴许版本号都已经内置了寻路AI了.之前的文章有介绍 Unity3d 寻路功能 介绍及项目演示 然而两年来项目中一直使用的是 A* Pathfinding 这个插件的.所以抽时间来写下 ...
- appium desktop
Appium-desktop 下载地址:https://github.com/appium/appium-desktop/releases 一般功能 这些能力跨越多个驱动因素. 仅限Android 这 ...
- python爬虫【第2篇】【多进程】
一.多进程 1.fork方法(os模块,适用于Lunix系统) fork方法:调用1次,返回2次.原因:操作系统经当前进程(父进程)复制出一份进程(子进程),两个进程几乎完全相同,fork方法分别在父 ...
- [计算机故障处理]EXCEL文件双击不能直接打开
同事的电脑里的EXCEL文件不知什么原因双击不能直接打开了,双击只能打开软件而且是没有任何表格的,但通过软件中的“打开”再找到指定的文件能打开. 解决方案: 打开excel,依次选择:工具-选项-常规 ...