codeforces680B之Bear and Finding Criminals

本文介绍了一种警察利用罪犯探测仪确定罪犯位置的算法。通过分析探测仪提供的距离信息,警察能准确锁定部分罪犯所在城市。文章通过两个实例详细解释了算法的工作原理,并提供了实现代码。

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

B. Bear and Finding Criminals
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
6 3
1 1 1 0 1 0
output
3
input
5 2
0 0 0 1 0
output
1
Note

In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.

Using the BCD gives Limak the following information:

  • There is one criminal at distance 0 from the third city — Limak is sure that this criminal is exactly in the third city.
  • There is one criminal at distance 1 from the third city — Limak doesn't know if a criminal is in the second or fourth city.
  • There are two criminals at distance 2 from the third city — Limak is sure that there is one criminal in the first city and one in the fifth city.
  • There are zero criminals for every greater distance.

So, Limak will catch criminals in cities 13 and 5, that is 3 criminals in total.

In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.

题意:警察要抓罪犯,有n个城市,1代表该城市有罪犯,0表示没有,警察在城市a,警察用可以定位罪犯位置的探测仪了解罪犯位置,这个检测仪只能同时检测两边的罪犯,

问最多可以知道多少个罪犯的位置????

数据分析:

6 3
1 1 1 0 1 0
第一次警察在3城市检测和他距离为0的城市,有一个罪犯,第二次检测和他距离为1的城市,一边有一边没有,无法判断,第三次检测和他距离为2的城市,一边一个,即有两个;所以警察知道1,3,5城市有罪犯,共3个
5 2
0 0 0 1 0
第一次警察在2城市检测和他距离为0的城市,没有罪犯,                           第二次检测和他距离为1的城市没有罪犯,
第三次检测和他距离为2的城市,只有一个城市,一个罪犯,所以可以检测到这个罪犯,  第四次没有
所以警察知道4城市有罪犯,共1个


很简单的题!!!比较a和n/2的位置即可

AC代码如下:
#include "iostream"
using namespace std;


int main(int argc, char* argv[])
{
	int s[110];
	int n,a,i;
	int count=0;
	cin>>n>>a;
	for (i=1;i<=n;i++)
		cin>>s[i];
	if (a<=n/2)
	{
		for (i=1;i<=n;i++)
		{
			if (i<a)
			{
				if (s[i]==1&&s[2*a-i]==1) count+=2;
			}
			else if (i==a)
			{
				if (s[i]==1) count++;
			}
			else
			{
				if (i>=2*a){ if(s[i]==1) count++; }
			}
		}
	}
	else
	{
		for (i=1;i<=n;i++)
		{
			if (i>=2*a-n&&i<a)
			{
				if (s[i]==1&&s[2*a-i]==1) count+=2;
			}
			else if (i==a)
			{
				if (s[i]==1) count++;
			}
			else
			{
				if (i<2*a-n){ if(s[i]==1) count++; }
			}
		}
	}
	cout<<count<<endl;
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值