题解归档 - cf888A
本文最后由方少年更新于2026 年 6 月 28 日,已超过0天没有更新。如果文章内容或图片资源失效,请留言反馈,将会及时处理,谢谢!
题解归档 - cf888A
本文由 cf-code 本地题解库自动归档;公开内容以本地 AC/验证版本为准。
- 本地编号:
cf888A - 本地来源:
problems/cf888A/idea.md - 题目链接:https://codeforces.com/contest/888/problem/A
- 原始标题:cf888A — Local Extrema
思路
cf888A — Local Extrema
题意
统计数组中局部极值个数:位置 i(2≤i≤n-1)为局部极小当 a[i]<a[i-1] 且 a[i]<a[i+1];局部极大当严格大于两侧邻居。
做法
遍历 i=2..n-1,判断是否为严格局部极小或极大,计数即可。O(n)。
结论
Educational Round 32 A,实现题,无额外模板沉淀。
代码
来源:problems/cf888A/solution.cpp
/* Author: likely
* Time: 2026-06-08 03:39:20
**/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll s[1005];
ll n,i,j,k,ans,cnt;
int main(){
cin>>n;
for(i=1;i<=n;i++) cin>>s[i];
ans=0;
for(i=2;i<=n-1;i++){
if((s[i]<s[i-1] and s[i]<s[i+1]) or (s[i]>s[i-1] and s[i]>s[i+1])) ans++;
}
cout<<ans<<"\n";
return 0;
}
~ ~ The End ~ ~
文章标题:题解归档 - cf888A
文章链接:https://www.fangshaonian.cn/archives/387/
最后编辑:2026 年 6 月 28 日 19:08 By 方少年
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)