Codeforces Round #356 (Div. 2)-B
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.
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.
Print the number of criminals Limak will catch.
6 3
1 1 1 0 1 0
3
5 2
0 0 0 1 0
1
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 2from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
题意:这题实在太难读啊。大概是有N个城市,每个城市最多只有1个罪犯,警察现在在第a个城市,现在警察有一个道具BCD,可以检查出距离a城市abs(i-a)的罪犯数量,现在问警察可以确定的罪犯的数目是多少?
思路:看懂题目或者例子说明后,直接模拟就可以了。 双指针L,R。如果L,R离a的位置相等,并且道具BCD都从L,R检测到罪犯,那么就可以肯定L,R都有一个罪犯。如果有一个指针的位置检测到罪犯,但是另一个指针已经越界了。那么可以肯定罪犯一定在第一个指针的位置中。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=+;
typedef long long int LL;
#define INF 0x3f3f3f3f
int T[MAXN],vis[MAXN];
int n,p;
bool check(int pos){
return pos>=&&pos<=n;
}
int main()
{
while(~scanf("%d%d",&n,&p)){
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
scanf("%d",&T[i]);
}
int ans=T[p]?:;
for(int L=p-,R=p+;L>=||R<=n;L--,R++){
if(check(L)&&check(R)){
ans+=(T[L]&&T[R]?:);
}
else{
if(!check(L)&&check(R)&&T[R]){
ans++;
}
if(check(L)&&!check(R)&&T[L]){
ans++;
}
}
}
printf("%d\n",ans);
}
return ;
}
Codeforces Round #356 (Div. 2)-B的更多相关文章
- Codeforces Round #356 (Div. 2)-A
A. Bear and Five Cards 题目链接:http://codeforces.com/contest/680/problem/A A little bear Limak plays a ...
- Codeforces Round #356 (Div. 2) C. Bear and Prime 100(转)
C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #356 (Div. 2)B. Bear and Finding Criminals(水题)
B. Bear and Finding Criminals time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces Round #356 (Div. 2)A. Bear and Five Cards(简单模拟)
A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 并查集+bfs+暴力滑窗 Codeforces Round #356 (Div. 2) E
http://codeforces.com/contest/680/problem/E 题目大意:给你一个n*n的图,然后图上的 . (我们下面都叫做‘点’)表示可以走,X表示不能走,你有如下的操作, ...
- dfs Codeforces Round #356 (Div. 2) D
http://codeforces.com/contest/680/problem/D 题目大意:给你一个大小为X的空间(X<=m),在该空间内,我们要尽量的放一个体积为a*a*a的立方体,且每 ...
- Codeforces Round #356 (Div. 1) D. Bear and Chase 暴力
D. Bear and Chase 题目连接: http://codeforces.com/contest/679/problem/D Description Bearland has n citie ...
- Codeforces Round #356 (Div. 2) E. Bear and Square Grid 滑块
E. Bear and Square Grid 题目连接: http://www.codeforces.com/contest/680/problem/E Description You have a ...
- Codeforces Round #356 (Div. 2) D. Bear and Tower of Cubes dfs
D. Bear and Tower of Cubes 题目连接: http://www.codeforces.com/contest/680/problem/D Description Limak i ...
随机推荐
- 【Git】笔记5 分支管理2
来源:廖雪峰 通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,Git就会在merge时生成一 ...
- js正则匹配以固定格式结尾的字符串并匹配是手机访问,则跳转
<script> //var pcUrl = "http://res.meadin.com/HotelData/98986_1.shtml"; var pcUrl = ...
- pod install 慢
最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的时候会升级Co ...
- storyboard pushViewController 的时候,新的界面黑屏
storyboard 创建的一级界面需要通过代码跳转到另一 storyboard 创建的界面的时候,通常我们会这样 其实 alloc init 相当于重新创建一个界面,所以我们 push 进入之后会发 ...
- supersr--控制器的生命周期:
调用顺序为 1 init函数(init;initWithFrame;initWithCoder;等)--初始化 2 awakeFromNib--在loadView之前的工作放在这里 3 viewDid ...
- cuda 初学大全
转自:http://blog.csdn.net/augusdi/article/details/12529331 cuda 初学大全 1 硬件架构CUDA编程中,习惯称CPU为Host,GPU为Dev ...
- clustershell
.安装 yum install clustershell .配置ssh无密码登录 .配置/etc/hosts 在hosts中文件中将ip和主机名对应起来,使用比较方便 .配置关键文件 clusters ...
- BeatifulSoup文档地址
http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
- IOS管理文件和目录NSFileManager
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- 设计模式之构建者模式(Builder):初步理解
构建者(Builder)设计模式(又叫生成器设计模式): 当一个类的内部数据过于复杂的时候(通常是负责持有数据的类,比如Config.VO.PO.Entity...),要创建的话可能就需要了解这个类的 ...