贪心+构造 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数组标记进去的读者,分情况 ...
随机推荐
- kibana启动--nohup在关闭终端后无效&&守护进程详解
https://blog.csdn.net/ty_0930/article/details/70184705 https://blog.csdn.net/Dream_Flying_BJ/article ...
- laravel 邮件
本文使用qq邮箱 env邮件配置: MAIL_FROM_ADDRESS = 17******92@qq.com MAIL_FROM_NAME = listen~ MAIL_DRIVER=smtp MA ...
- 【转】c++中placement new操作符
new:指我们在C++里通常用到的运算符,比如A* a = new A; 对于new来说,有new和::new之分,前者位于std operator new():指对new的重载形式,它是一个函数, ...
- Linux学习系列之LNMP
LNMP介绍 LNMP是什么 LNMP(Linux-Nginx-MySQL-PHP)网站架构是目前国际流行的Web架构; 这四种软件组合,可以成为一个免费.高效.扩展性强的Web架构; LNMP原理图 ...
- Jenkins 使用
Jenkins 安装 Jenkins是用Java语言开发的系统,首先要确定服务器上已经安装JDK或者JRE. 安装方式一 直接运行java –jar Jenkins.war,在浏览器中输入 http: ...
- PHP + Socket 发送http请求进而实现站点灌水
本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...
- Android 网络学习之获取server文本文件
上次我们学习怎样从网络上获取一张图片,今天我们学习怎样从网络上获取文本文件.以XML文件为样例. 由于XML文件在实际开发中最为常见. 我们以以下图片为样例学习怎样从网络上获取XML文件 我们的xml ...
- 大数据R语言简析
R语言是用于统计分析.画图的语言和操作环境.R是属于GNU系统的一个自由.免费.源码开放的软件.它是一个用于统计计算和统计制图的优秀工具. R是统计领域广泛使用的诞生于1980年左右的S语言的一个分支 ...
- 【bzoj1798】[Ahoi2009]Seq 维护序列seq
大意:一个数组,三个操作,第一种是区间[a,b]每个数乘乘,第二种是区间[a,b]每个数加c,第三种是查询[a,b]区间的和并对p取摸. 两种操作就不能简单的只往下传标记.每次传乘法标记时,要把加法标 ...
- 【bzoj4196】[Noi2015]软件包管理器
裸的树链剖分. 对于安装 查询和维护到根路径 对于卸载 查询和维护子树信息 一开始线段树add[]标记要全赋值为-1 #include<algorithm> #include<ios ...