#include<bits/stdc++.h> using namespace std; using ll=long long; constexpr int MAX=1e9+1e6; constexpr int MIN=-1e9-1e6; int n,temp; int res=0; int a[50001]; int t[50001]; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n; for(int i=0;i<n;++i) cin>>t[i],a[i]=t[i]; bool ok=0; for(int i=1;i<n;++i){ if(ok and a[i-1]>=a[i]){ ++temp; a[i]=MAX; } else if(!ok and a[i-1]<=a[i]){ ++temp; a[i]=MIN; } ok=!ok; } ok=1; for(int i=0;i<n;++i) a[i]=t[i]; for(int i=1;i<n;++i){ if(ok and a[i-1]>=a[i]){ ++res; a[i]=MAX; } else if(!ok and a[i-1]<=a[i]){ ++res; a[i]=MIN; } ok=!ok; } cout<<min(res,temp); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include<bits/stdc++.h> using namespace std; using ll=long long; constexpr int MAX=1e9+1e6; constexpr int MIN=-1e9-1e6; int n,temp; int res=0; int a[50001]; int t[50001]; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin>>n; for(int i=0;i<n;++i) cin>>t[i],a[i]=t[i]; bool ok=0; for(int i=1;i<n;++i){ if(ok and a[i-1]>=a[i]){ ++temp; a[i]=MAX; } else if(!ok and a[i-1]<=a[i]){ ++temp; a[i]=MIN; } ok=!ok; } ok=1; for(int i=0;i<n;++i) a[i]=t[i]; for(int i=1;i<n;++i){ if(ok and a[i-1]>=a[i]){ ++res; a[i]=MAX; } else if(!ok and a[i-1]<=a[i]){ ++res; a[i]=MIN; } ok=!ok; } cout<<min(res,temp); } |