这次CF不是很难,我这种弱鸡都能在半个小时内连A四道……不过E题没想到还有这种折半+状压枚举+二分的骚操作,后面就挂G了……
A.Local Extrema
题目链接:https://cn.vjudge.net/problem/CodeForces-888A
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
3
1 2 3
0
4
1 5 2 5
2
AC代码:
#include<bits/stdc++.h> using namespace std; int n,num[1005],cnt; bool local_extremum(int a,int b,int c){return (a<b && b>c)||(a>b && b<c);} int main() { cin>>n; for(int i=1;i<=n;i++) scanf("%d",&num[i]); cnt=0; for(int i=2;i<=n-1;i++) { if(local_extremum(num[i-1],num[i],num[i+1])) cnt++; } cout<<cnt<<endl; }