码迷,mamicode.com
首页 > 其他好文 > 详细

D - Bear and Finding Criminals

时间:2016-07-11 10:31:16      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

D - Bear and Finding Criminals

标签:

原文地址:http://www.cnblogs.com/Kiven5197/p/5659211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!