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=100+5; typedef long long int LL; #define INF 0x3f3f3f3f int T[MAXN],vis[MAXN]; int n,p; bool check(int pos){ return pos>=1&&pos<=n; } int main() { while(~scanf("%d%d",&n,&p)){ memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++){ scanf("%d",&T[i]); } int ans=T[p]?1:0; for(int L=p-1,R=p+1;L>=1||R<=n;L--,R++){ if(check(L)&&check(R)){ ans+=(T[L]&&T[R]?2:0); } else{ if(!check(L)&&check(R)&&T[R]){ ans++; } if(check(L)&&!check(R)&&T[L]){ ans++; } } } printf("%d\n",ans); } return 0; }