AcWing 3227. 折点计数
原题链接
简单
作者:
牛奶小柒Luke
,
2021-02-19 22:07:55
,
所有人可见
,
阅读 265
数组存储
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1010;
int n;
int a[N];
int main(){
scanf("%d",&n);
for(int i = 0;i < n;++i) scanf("%d",&a[i]);
int ans = 0;
for(int i = 1;i < n - 1;++i){
if((a[i - 1] < a[i] && a[i] > a[i + 1]) || (a[i - 1] > a[i] && a[i] < a[i + 1])) ans++;
}
printf("%d",ans);
return 0;
}
判断单调性
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int n,ans = 0;
int main(){
cin >> n;
int left,mid,right;
if(cin >> left >> mid){
for(int i = 3;i <= n;++i){
cin >> right;
if(left < mid && mid > right) ans++;
else if(left > mid && mid < right) ans++;
left = mid;
mid = right;
}
}
cout << ans << endl;
return 0;
}
单调区间与区间之间比较,如果不同则为折点
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,ans = 0;
int first,second,prearrow,arrow;
int main(){
cin >> n;
cin >> first;
if(cin >> second){
prearrow = (second - first > 0) ? 1 : 0;
first = second;
for(int i = 3;i <= n;++i){
cin >> second;
arrow = (second - first > 0) ? 1 : 0;
if(prearrow != arrow) ans++;
prearrow = arrow;
first = second;
}
}
cout << ans << endl;
return 0;
}