Codeforces Round #356 (Div. 2) B. Bear and Finding Criminal 水题
B. Bear and Finding Criminals
题目连接:
http://www.codeforces.com/contest/680/problem/B
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
6 3
1 1 1 0 1 0
Sample Output
3
Hint
题意
有6个城市,有一个警察在a,有一个探测器,可以探测到距离他为i的地方有多少个歹徒。
现在给你每个城市的歹徒数量,最多为1
问你这个警察能够确认歹徒的所在的歹徒,一共有多少个
题解:
模拟一下就好了,如果左右都没有越界的话,那么必须左右都得有歹徒
否则就必须其中一边越界才行。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
int n,a,p[maxn];
int main()
{
scanf("%d%d",&n,&a);
for(int i=1;i<=n;i++)
scanf("%d",&p[i]);
int ans = p[a];
for(int i=0,l=a-1,r=a+1;l>=1||r<=n;l--,r++){
if(l>=1&&r<=n){
if(p[l]&&p[r])ans+=2;
}
else if(l>=1)ans+=p[l];
else if(r<=n)ans+=p[r];
}
cout<<ans<<endl;
}