★【16.6.2】Codeforces Round #356 (Div. 2) B. Bear and Finding Criminals
2016-06-09 04:19
281 查看
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 citya.
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
output
input
output
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 1, 3 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
题意:每次判断离出发点范围距离多少的点,不能确定总数不加。确定的方案:两边都是1,一边是1,另一边超出范围、
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
int i,j;
int n,m,d,sum;
int a[9999];
int main()
{
while(~scanf("%d%d",&n,&m))
{
//printf("%d\n",sum);
sum=0;
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
d=m-i;
if(m+d>n&&a[i]==1)
{
sum++;
}
else if(a[i]==1&&m+d<=0)
{
sum++;
}
else if(a[i]==1&&a[m+d]==1)
{
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
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 citya.
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 1, 3 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
题意:每次判断离出发点范围距离多少的点,不能确定总数不加。确定的方案:两边都是1,一边是1,另一边超出范围、
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
int i,j;
int n,m,d,sum;
int a[9999];
int main()
{
while(~scanf("%d%d",&n,&m))
{
//printf("%d\n",sum);
sum=0;
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
d=m-i;
if(m+d>n&&a[i]==1)
{
sum++;
}
else if(a[i]==1&&m+d<=0)
{
sum++;
}
else if(a[i]==1&&a[m+d]==1)
{
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
相关文章推荐
- 框架 day49 BOS项目练习3(修复window控件BUG,添加/修改/作废取派员,datagrid使用,分页查询(DetachedCriteria离线),formatter函数)
- [TypeScript] Stopping a TypeScript Build When Errors Are Found
- 设计模式-抽象工厂和builder模式
- mysql主从复制中的半同步复制
- 2016 UESTC Training for Math Problem F-粗心的谭爷(线性筛素数)
- python中的and | or表达式
- 2016 UESTC Training for Math Problem D-熄灯啦!(乱搞)
- 2016 UESTC Training for Math Problem C-咸鱼的跑步比赛(解同余线性方程组)
- CentOS下安装MySQL
- 2016 UESTC Training for Math Problem B-三角形棋盘上的博弈游戏(打表+DP)
- 2016 UESTC Training for Math Problem A-谭爷的黑暗沙拉(组合数学)
- 创业吧,创业!【2016-06-09】
- codeforces 356 div2 C.Bear and Prime 100 数学
- 程序性能的初步优化与分析(以 C++ 为例)
- python中的列表生成式 | 字典生成式
- 圆形(和柱形)动态增加的自定义view的制作
- MyBatis调用存储过程返回结果集
- [leetcode] 354. Russian Doll Envelopes 解题报告
- 统计学 入门基础概念篇 Probability 概率部分 (个人笔记)
- leetcode 3. Longest Substring Without Repeating Characters My Submissions QuestionEditorial Solution