CodeForces 888A Local Extrema

Description:

You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.

An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.

Input

The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the elements of array a.

Output

Print the number of local extrema in the given array.

Example
Input
3
1 2 3
Output
0
Input
4
1 5 2 5
Output
2

題目大意:

給定一個數組, 它定義的局部極值爲最大/小值爲當前這個數字比左右臨近數字都要大/小, 現在讓你統計共有多少個局部極值。

解題思路:

對於第一個數字和最後一個數字一定是不爲局部極值的, 所以我們從第二個數字開始遍歷到第n-1個, 其中如果有滿足條件的數字的話就統計上, 最後輸出總數即可。

代碼:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>

using namespace std;

/*
 *ios::sync_with_stdio(false);
 */

typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 1e6;

int n;
int arr[Max];

int main() {
    int cnt = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &arr[i]);
    }
    for (int i = 1; i < n - 1; ++i) {
        if (arr[i - 1] < arr[i] && arr[i + 1] < arr[i])
            cnt++;
        else if (arr[i - 1] > arr[i] && arr[i + 1] > arr[i])
            cnt++;
    }
    printf("%d\n", cnt);
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章