日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Bear and Finding Criminals (模拟)

發(fā)布時(shí)間:2025/4/16 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Bear and Finding Criminals (模拟) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

?

大致題意:一個(gè)警察住在某個(gè)城市,他要抓各個(gè)城市的小偷,現(xiàn)在用一個(gè)BCD可以知道那個(gè)城市里一定有小偷。

? ? ? ? ? ? ? ? ? (一定能確定該城市有小偷的幾種情況:

? ? ? ? ? ? ? ? ? ?? 1.警察所住城市有小偷,則一定能檢測(cè)到

? ? ? ? ? ? ? ? ? ?? 2.警察所住城市的左面和右面位置若都不為0,則說明兩座城市都有小偷

? ? ? ? ? ? ? ? ? ?? 3.警察所在城市的一邊檢測(cè)到有小偷,但在另一邊已經(jīng)沒有城市了,則說明該城市一定有小偷)

?

第一次做的時(shí)候,從警察所在城市向兩邊擴(kuò)展,判斷兩邊所存數(shù)組的值是否為0,結(jié)果測(cè)試到第15組數(shù)據(jù)是出錯(cuò),后來才發(fā)現(xiàn),忽略了數(shù)組之外的值,他們的值是隨機(jī)的。

錯(cuò)誤代碼

#include<cstdio> #include<cstring> #include<iostream>using namespace std;const int MAXN = 1000; int t[MAXN];int main() {int n, a;while(scanf("%d%d",&n, &a)!=EOF){int cnt = 0;memset(t, 0, sizeof(t));for(int i =1; i <= n; i++)cin >> t[i];if(t[a]) cnt++;cout << cnt <<endl;for(int i = 1; i <= n; i++){if(t[a-i] >= 1&&t[a+i] == 1) { //******錯(cuò)誤點(diǎn) cnt+=2;cout << cnt;}else if(a-i <= 0&&a+i <= n){if(t[a+i]) cnt++;cout << cnt ;}else if(a-i >= 1&&a+i > n){if(t[a-i]) cnt++;cout << cnt ;}printf(" %d\n",cnt);}} }

將錯(cuò)誤的地方改正?

#include<cstdio> #include<cstring> #include<iostream>using namespace std;const int MAXN = 1000; int t[MAXN];int main() {int n, a;while(scanf("%d%d",&n, &a)!=EOF){int cnt = 0;//memset(t, 0, sizeof(t));for(int i =1; i <= n; i++)cin >> t[i];if(t[a]) cnt++;for(int i = 1; i <= n; ++i){if(a-i > 0&&a+i <= n) { //******改正錯(cuò)誤點(diǎn) if(t[a-i] == 1&&t[a+i] == 1)cnt+=2; }else if(a-i <= 0&&a+i <= n){if(t[a+i])cnt++;} else if(a-i > 0&&a+i > n){if(t[a-i])cnt++; } }cout << cnt <<endl;} }

?

總結(jié)

以上是生活随笔為你收集整理的Bear and Finding Criminals (模拟)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。