CUC-SUMMER-6-G

G - Bear and Finding Criminals
CodeForces-680B

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 ticriminals in the i-th city.

Output
Print the number of criminals Limak will catch.

Example
Input
6 31 1 1 0 1 0

Output
3

Input
5 20 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.



題意:在一維的坐標軸上,警察住在n位置,儀器會告訴你距離為k的地方有幾個犯人,問警察最多能確定幾個犯人

解法:從n向兩側遍歷,如果兩邊都沒到頭能判斷0和2,一端到頭之后能判斷0和1

代碼:

#include<iostream>
using namespace std;
int c[105];
int main()
{
    int n,a;
    cin>>n>>a;
    for(int i=1;i<=n;i++)
        cin>>c[i];
    int k=1;
    int ans=c[a]?1:0;
    while(1){
        if(a-k>0&&a+k<=n){
            ans+=(c[a-k]&c[a+k])*2;
        }
        else if(a-k<1&&a+k<=n)
            ans+=c[a+k];
        else if(a-k>=1&&a+k>n)
            ans+=c[a-k];
        else
            break;
        k++;
    }
    cout<<ans<<endl;
    return 0;
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,043評論 0 23
  • 大學出來也有3年了,在這幾年間我變了很多,很多,能力方面看似還不錯,然而我已經(jīng)停滯了很久,變得越來越不知所措,不知...
    LandisWhite閱讀 262評論 0 2
  • 憑誰問端午,屈子沉江湖。 斯水已東逝,魂兮得安無?
    米速閱讀 211評論 0 1
  • 1.新作 你不要去勸諫 勸勉一個死心的人 他的神采奕奕僅僅簇擁在權利面前 美色 錢財竟然一時間一概不顧 墓門上棗樹...
    陳恕之閱讀 361評論 0 1
  • 魯迅文學獎獲獎同名小說改編。 新晉導演“楊子”憑借《喊山》入圍美國首屆#金色銀幕獎#最佳導演及編劇提名。 兩位主要...
    薇董閱讀 338評論 1 1

友情鏈接更多精彩內(nèi)容