D - Bear and Finding Criminals

本文介绍了一种基于罪犯探测器(BCD)的算法,该算法帮助警察Limak确定并捕捉分布在不同城市中的罪犯。通过分析罪犯在各城市的分布情况,Limak能够精确判断出哪些城市的罪犯可以被捕捉。

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 3
1 1 1 0 1 0
Output
3
Input
5 2
0 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 }

 

转载于:https://www.cnblogs.com/Kiven5197/p/5659211.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值