D - Bear and Finding Criminals

Description

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.

Sample Input

Input

6 31 1 1 0 1 0

Output

3

Input

5 20 0 0 1 0

Output

1

题意:共有n座城市,Limak在第a座城市,每做城市的距离即序列号相减的绝对值。米格城市最多有一名罪犯,BCD可以告诉Limak在某个距离处有多少罪犯,如果Limak确定罪犯在哪个城市,就可以抓到他,求他可以抓住多少罪犯。

如:

1 1 1 0 1 0   Limak在第三位,则BCD可知距离为1处有1名罪犯,则Limak不能确定在哪个城市故无法抓到;距离为2处有两名,则确定每个城市必有一名,故抓到两名;距离为3处有一名,因为距离为3 的城市只有一座,也能确定,故抓住一名。

附AC代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 int n,m;
 8 int a[110];
 9
10 int main(){
11     while(cin>>n>>m){
12         int sum=0;
13         memset(a,0,sizeof(a));
14         for(int i=1;i<=n;i++){
15             cin>>a[i];
16         }
17         for(int i=0;i<n;i++){
18             if(m-i<1&&m+i<=n){
19                 if(a[m+i]==1){
20                     sum++;
21                     continue;
22                 }
23             }
24             if(m+i>n&&m-i>=1){
25                 if(a[m-i]==1){
26                     sum++;
27                     continue;
28                 }
29             }
30             if(a[m-i]==1&&a[m+i]==1){
31                 if(m-i==m+i){
32                     sum++;
33                     continue;
34                 }
35                 else{
36                     sum+=2;
37                     continue;
38                 }
39
40             }
41         }
42         cout<<sum<<endl;
43     }
44     return 0;
45 }
时间: 2024-12-27 23:25:54

D - Bear and Finding Criminals的相关文章

CodeForce 680B - Bear and Finding Criminals

Bear and Finding Criminals 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 crimin

codeforces 680B B. Bear and Finding Criminals(水题)

题目链接: B. Bear and Finding Criminals //#include <bits/stdc++.h> #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstd

Codeforces Round #356 (Div. 2) [Codeforces680]

此处有目录↑ Codeforces Round #356(Div. 2):http://codeforces.com/contest/680 A. Bear and Five Cards (贪心) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A little bear Limak plays a game. He has

codeforces 653B B. Bear and Compressing(dfs)

题目链接: B. Bear and Compressing time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Limak is a little polar bear. Polar bears hate long strings and thus they like to compress them. You should al

codeforces 653A A. Bear and Three Balls(水题)

题目链接: A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each

POJ 2049 Finding Nemo 优先队列 STL

题目链接:http://poj.org/problem?id=2049 题目利用了<海底总动员>的情节,小丑鱼尼莫迷路了,他老爸去营救他便是题意. 题目给出了这样的地图,说是假设地图由墙和门组成,忽略墙的厚度,地图上有门,没有墙的地方是可以自由行动的问可以经过最少多少道门便可以营救到尼莫. 这个题给的数据是墙的交点为整数点,但鱼爸爸实在非墙的地方自由移动. 因此,这个题有两个难点: 1.如果建图保存地图 2.如何在地图上遍历 由于题目是给出一个点(x,y),来表示一段墙 我便用一对X,Y来表示

Codeforces Round #226 (Div. 2)--A Bear and Raspberry

题目链接:Bear and Raspberry Bear and Raspberry time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The bear decided to store some raspberry for the winter. He cunningly found out the price for a ba

Finding Lines

Finding Lines 题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4967 概率 在听题解前毫无头绪,题解帮我打开了新世界的大门: 随机取一个点在答案直线(如果存在这个直线)上的概率是p%, 那么随机取到两个点构成的直线就是答案直线的概率是p%*p%: 也就是说,随机取到两个点构成的直线不是答案直线的概率为

基于分治法的找秩(maxium rank finding)策略

rank finding 问题的概述如下: 在一群离散的点中,检查对于每个点,是否有x值和y值都小于它的点,若有,则该点的秩为这些小于它的点的数量. 分治法思想: 以界面上所有点中x值的中位数为基准,递归地把目标区域分成两部分,直到每个区域中的点在一个以下,对于每一个细小的区域,检查其left child区域中的点的y值和right child区域中的点的y值,以对right child中的点的秩进行统计. 该方法通过分治排除了水平位置不符合标准的点,有效的减少了统计次数. 代码示例如下: #i