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

本文解析了Codeforces B题“B. Bear and Finding Criminals”的背景及算法实现思路。介绍了如何利用模拟算法来确定警察能够确认的歹徒位置,通过遍历城市并计算能够确定的歹徒数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值