• CodeForce 680B


    Bear and Finding Criminals

    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 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.

    题意:

    给出小偷的位置p, 然后开始偷跑,

    分2种情况。

    一.与位置p相距i单位的左边,右边城市都有罪犯,那么随便跑哪个城市都能抓到罪犯,且两个都能抓到。
    二.与位置p相距i单位的左边(右边)没有城市,但是对称的右边(左边)有城市,且城市里面有罪犯,也就是有罪犯的城市唯一,那么这个罪犯就一定能被抓到。
    AC代码:
    # include <iostream>
    using namespace std;
    int a[105];
    int main()
    {
        int n, p, ans;
        while(cin >> n >> p)
    	{
            for(int i = 0; i < n; i++)
    		{
                cin >> a[i];
            }
            ans = 0, p--;
            if(a[p]) 
    			ans++;     
            for(int i = 1 ;i < n ; i++)
    		{
                if(p + i >= n && p - i < 0)      
    				break;   
                if(p + i >= n && a[p-i]) 
    				ans++;   
                else if(p - i < 0 && a[p+i]) 
    				ans++; 
                else if(p + i < n && p - i >= 0 && a[p-i] && a[p+i]) 
    				ans += 2;
            }
            cout << ans << endl;
        }
        return 0;
    }
    
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    Qt编写ffmpeg本地摄像头显示(16路本地摄像头占用3.2%CPU)
    Qt编写本地摄像头综合应用示例(qcamera/ffmpeg/v4l2等)
    Qt编写音频播放示例(带音频曲线/振幅/传输/录制等)
    Qt编写跨平台RTSP/RTMP/HTTP视频流播放器
    Qt编写4K/8K大分辨率播放器(8K占用1%CPU)
    Qt编写的视频播放综合应用示例(qmedia/ffmpeg/vlc/mpv/海康sdk等)
    Qt编写linux上视频流播放器(支持海康大华宇视等各种网络摄像机)
    Qt编写视频监控显示安卓版
    Qt音视频开发02海康sdk解码(支持句柄/回调/gpu模式/支持win/linux)
    Qt音视频开发01共享解码线程(耗时一年/性能凶残/至臻完美)
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5786808.html
Copyright © 2020-2023  润新知