B. Bear and Finding Criminals
链接:http://codeforces.com/contest/680/problem/B

There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.

Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.

Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.

You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.

Input

The first line of the input contains two integers n and a (1 ≤ a ≤ n ≤ 100) — the number of cities and the index of city where Limak lives.

The second line contains n integers t1, t2, ..., tn (0 ≤ ti ≤ 1). There are ti criminals in the i-th city.

Output

Print the number of criminals Limak will catch.

Examples
input
6 3
1 1 1 0 1 0
output
3
input
5 2
0 0 0 1 0
output
1
Note

In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.

Using the BCD gives Limak the following information:

  • There is one criminal at distance 0 from the third city — Limak is sure that this criminal is exactly in the third city.
  • There is one criminal at distance 1 from the third city — Limak doesn't know if a criminal is in the second or fourth city.
  • There are two criminals at distance 2 from the third city — Limak is sure that there is one criminal in the first city and one in the fifth city.
  • There are zero criminals for every greater distance.

So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.

In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.

题意:这题实在太难读啊。大概是有N个城市,每个城市最多只有1个罪犯,警察现在在第a个城市,现在警察有一个道具BCD,可以检查出距离a城市abs(i-a)的罪犯数量,现在问警察可以确定的罪犯的数目是多少?

思路:看懂题目或者例子说明后,直接模拟就可以了。 双指针L,R。如果L,R离a的位置相等,并且道具BCD都从L,R检测到罪犯,那么就可以肯定L,R都有一个罪犯。如果有一个指针的位置检测到罪犯,但是另一个指针已经越界了。那么可以肯定罪犯一定在第一个指针的位置中。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=+;
typedef long long int LL;
#define INF 0x3f3f3f3f
int T[MAXN],vis[MAXN];
int n,p;
bool check(int pos){
return pos>=&&pos<=n;
}
int main()
{
while(~scanf("%d%d",&n,&p)){
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
scanf("%d",&T[i]);
}
int ans=T[p]?:;
for(int L=p-,R=p+;L>=||R<=n;L--,R++){
if(check(L)&&check(R)){
ans+=(T[L]&&T[R]?:);
}
else{
if(!check(L)&&check(R)&&T[R]){
ans++;
}
if(check(L)&&!check(R)&&T[L]){
ans++;
}
}
}
printf("%d\n",ans);
}
return ;
}

Codeforces Round #356 (Div. 2)-B的更多相关文章

  1. Codeforces Round #356 (Div. 2)-A

    A. Bear and Five Cards 题目链接:http://codeforces.com/contest/680/problem/A A little bear Limak plays a ...

  2. Codeforces Round #356 (Div. 2) C. Bear and Prime 100(转)

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Codeforces Round #356 (Div. 2)B. Bear and Finding Criminals(水题)

    B. Bear and Finding Criminals time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  4. Codeforces Round #356 (Div. 2)A. Bear and Five Cards(简单模拟)

    A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. 并查集+bfs+暴力滑窗 Codeforces Round #356 (Div. 2) E

    http://codeforces.com/contest/680/problem/E 题目大意:给你一个n*n的图,然后图上的 . (我们下面都叫做‘点’)表示可以走,X表示不能走,你有如下的操作, ...

  6. dfs Codeforces Round #356 (Div. 2) D

    http://codeforces.com/contest/680/problem/D 题目大意:给你一个大小为X的空间(X<=m),在该空间内,我们要尽量的放一个体积为a*a*a的立方体,且每 ...

  7. Codeforces Round #356 (Div. 1) D. Bear and Chase 暴力

    D. Bear and Chase 题目连接: http://codeforces.com/contest/679/problem/D Description Bearland has n citie ...

  8. Codeforces Round #356 (Div. 2) E. Bear and Square Grid 滑块

    E. Bear and Square Grid 题目连接: http://www.codeforces.com/contest/680/problem/E Description You have a ...

  9. Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs

    D. Bear and Tower of Cubes 题目连接: http://www.codeforces.com/contest/680/problem/D Description Limak i ...

随机推荐

  1. 【轮子】发现一个效果丰富酷炫的Android动画库

    没有什么比发现一个好轮子更让人开心的了. 这个库分分钟提高交互体验 :AndroidViewAnimations 一张图说明一切 配置和使用也相当简单 GitHub地址

  2. 51nod 1099 任务执行顺序 (贪心算法)

    题目:传送门. 题意:中文题. 题解:r[i]-o[i]值大的先进行.反证法:如果大的后进行,会导致空间增大,所以一定大的是先进行. #include <iostream> #includ ...

  3. 【XLL API 函数】 xlFree

    用于释放使用 Excel4,Excel4v,Excel12,Excel12v 分配的 XLOPER/XLOPER12 占用的内存资源. xlFree 函数释放辅助内存和重置指针为NULL但不释放XLO ...

  4. css样式设置图片半透明度,兼容IE8,火狐

    关于背景颜色透明的兼容浏览器的问题,一直是个问题,我所写的兼容IE8,和火狐,说是兼容所有浏览器我就没有测试,有兴趣的朋友可以自己测试下吧. background-color:white;filter ...

  5. mongodb3.x用户角色

    用户和角色是多对多的关系,一个用户可以对应多个角色,一个角色可以拥有多个用户.用户角色的不同对应的权限也是不一样的.下面是一些分配给用户的常见的角色. read                    ...

  6. Android Programming: Pushing the Limits -- Chapter 7:Android IPC -- ApiWrapper

    前面两片文章讲解了通过AIDL和Messenger两种方式实现Android IPC.而本文所讲的并不是第三种IPC方式,而是对前面两种方式进行封装,这样我们就不用直接把Aidl文件,java文件拷贝 ...

  7. Linux下pipe使用注意事项

    转自:http://blog.yufeng.info/archives/1485 Linux下的pipe使用非常广泛, shell本身就大量用pipe来粘合生产者和消费者的. 我们的服务器程序通常会用 ...

  8. Delphi面向对象编程

    一.面向对象介绍 OOP是使用独立的对象(包含数据和代码)作为应用程序模块的范例.虽然OOP不能使得代码容易编写,但是它能够使得代码易于维护.将数据和代码结合在一起,能够使定位和修复错误的工作简单化, ...

  9. 拷贝,集合,函数,enumerate,内置函数

    1.拷贝 字符串和数字.赋值 id一样 import copy #提供拷贝功能 copy.copy() #原来的和现在的一起修改,不用修改时用浅copy,节省内存,复制最外层 copy.deepcop ...

  10. 【php全局变量和静态变量、静态方法的使用方法】

    php全局变量使用关键字global声明,静态变量使用static声明,静态变量的使用可以使用 类名::变量名 示例代码: <?php //全局变量global 的用法和静态变量的使用 glob ...