#include <bits/stdc++.h> #define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define IMAX 1e9 #define IMIN -1e9 using namespace std; int n, t[50004], t1[50004], t2[50004], res1, res2; int main(){ iamspeed; cin >> n; for(int i = 1 ; i <= n ; i++){ cin >> t[i]; t1[i] = t2[i] = t[i]; } for(int i = 1 ; i < n ; i++){ if(i % 2 && t1[i] >= t1[i + 1]){ t1[i + 1] = IMAX; res1++; } else if(!(i % 2) && t1[i] <= t1[i + 1]){ t1[i + 1] = IMIN; res1++; } } for(int i = 1 ; i < n ; i++){ if(i % 2 && t2[i] <= t2[i + 1]){ t2[i + 1] = IMIN; res2++; } else if(!(i % 2) && t2[i] >= t2[i + 1]){ t2[i + 1] = IMAX; res2++; } } 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 | #include <bits/stdc++.h> #define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define IMAX 1e9 #define IMIN -1e9 using namespace std; int n, t[50004], t1[50004], t2[50004], res1, res2; int main(){ iamspeed; cin >> n; for(int i = 1 ; i <= n ; i++){ cin >> t[i]; t1[i] = t2[i] = t[i]; } for(int i = 1 ; i < n ; i++){ if(i % 2 && t1[i] >= t1[i + 1]){ t1[i + 1] = IMAX; res1++; } else if(!(i % 2) && t1[i] <= t1[i + 1]){ t1[i + 1] = IMIN; res1++; } } for(int i = 1 ; i < n ; i++){ if(i % 2 && t2[i] <= t2[i + 1]){ t2[i + 1] = IMIN; res2++; } else if(!(i % 2) && t2[i] >= t2[i + 1]){ t2[i + 1] = IMAX; res2++; } } cout << min(res1, res2) << endl; } |