#include <bits/stdc++.h> using namespace std; int arr[100005]; int brr[100005]; int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; for(int i=0;i<n;i++) cin>>arr[i]; int res1 = 0; for(int i=0;i<n;i++) brr[i] = arr[i]; for(int i=1;i<n;i++){ int s = i&1; if(brr[i-1] >= brr[i]){ if(s == 1) res1++, brr[i] = 1e9; } if(brr[i-1] <= brr[i]){ if(s == 0) res1++, brr[i] = -1e9; } } int res2 = 0; for(int i=0;i<n;i++) brr[i] = arr[i]; for(int i=1;i<n;i++){ int s = (i+1)&1; if(brr[i-1] >= brr[i]){ if(s == 1) res2++, brr[i] = 1e9; } if(brr[i-1] <= brr[i]){ if(s == 0) res2++, brr[i] = -1e9; } } cout<<min(res1, res2)<<endl; }
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 | #include <bits/stdc++.h> using namespace std; int arr[100005]; int brr[100005]; int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; for(int i=0;i<n;i++) cin>>arr[i]; int res1 = 0; for(int i=0;i<n;i++) brr[i] = arr[i]; for(int i=1;i<n;i++){ int s = i&1; if(brr[i-1] >= brr[i]){ if(s == 1) res1++, brr[i] = 1e9; } if(brr[i-1] <= brr[i]){ if(s == 0) res1++, brr[i] = -1e9; } } int res2 = 0; for(int i=0;i<n;i++) brr[i] = arr[i]; for(int i=1;i<n;i++){ int s = (i+1)&1; if(brr[i-1] >= brr[i]){ if(s == 1) res2++, brr[i] = 1e9; } if(brr[i-1] <= brr[i]){ if(s == 0) res2++, brr[i] = -1e9; } } cout<<min(res1, res2)<<endl; } |