XiaoHuang's Space
XiaoHuang's Space
XiaoHuang
May the force be with you.
『题解』Codeforces888A Local Extrema
Posted: Dec 11, 2019
Last Modified: Dec 13, 2019
This article was last modified days ago. The content of this post may be outdated!

Portal

Portal1: Codeforces

Portal2: Luogu

Description

You are given an array $a$. Some element of this array $a_i$ is a local minimum iff it is strictly less than both of its neighbours (that is, $a_i < a_i - 1$ and $a_i < a_i + 1$). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, $a_i > a_i - 1$ and $a_i > a_i + 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 \le n \le 1000)$ — the number of elements in array $a$.

The second line contains $n$ integers $a1, a2,\cdots , a_n (1 \le a_i \le 1000)$ — the elements of array $a$.

Output

Print the number of local extrema in the given array.

Sample Input1

3
1 2 3

Sample Output1

0

Sample Input2

4
1 5 2 5

Sample Output3

2

Solution

题目怎么说我们怎么做就可以了,循环直接从$2 \to n - 1$进行枚举。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

const int MAXN = 1005;
int n, a[MAXN];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    int ans = 0;
    for (int i = 2; i < n; i++)
        if (a[i] > a[i - 1] && a[i] > a[i + 1] || a[i] < a[i - 1] && a[i] < a[i + 1]) ans++;//按题目模拟
    printf("%d\n", ans);
    return 0;
}
Article License: CC BY-NC-ND 4.0
Article Author: XiaoHuang
  1. 1. Portal
  2. 2. Description
  3. 3. Input
  4. 4. Output
  5. 5. Sample Input1
  6. 6. Sample Output1
  7. 7. Sample Input2
  8. 8. Sample Output3
  9. 9. Solution
  10. 10. Code
Newer Post
『题解』洛谷P2357 守墓人
Older Post
『题解』Codeforces656E Out of Controls
Buy me a beer?
-->
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×