#include <bits/stdc++.h> using namespace std; const int PIN = 1e9; const int MIN = -1e9; int n, L1=0, L2=0, L3=0, L4=0, wmw[50500], mwm[50500]; string s; int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i=1; i<=n; i++) { cin >> wmw[i]; mwm[i]=wmw[i]; } for (int i=2; i<=n; i++) { if (i%2==0) { if(wmw[i-1]<=wmw[i]) { wmw[i]=MIN; L1++; } if (mwm[i-1]>=mwm[i]) { mwm[i]=PIN; L2++; } } if (i%2==1) { if(wmw[i-1]>=wmw[i]) { wmw[i]=PIN; L1++; } if (mwm[i-1]<=mwm[i]) { mwm[i]=MIN; L2++; } } } // for (int i=1; i<=n; i++) { // cout << mwm[i] << " " << wmw[i] << '\n'; // } cout << min(L1, L2); return 0; }
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 | #include <bits/stdc++.h> using namespace std; const int PIN = 1e9; const int MIN = -1e9; int n, L1=0, L2=0, L3=0, L4=0, wmw[50500], mwm[50500]; string s; int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i=1; i<=n; i++) { cin >> wmw[i]; mwm[i]=wmw[i]; } for (int i=2; i<=n; i++) { if (i%2==0) { if(wmw[i-1]<=wmw[i]) { wmw[i]=MIN; L1++; } if (mwm[i-1]>=mwm[i]) { mwm[i]=PIN; L2++; } } if (i%2==1) { if(wmw[i-1]>=wmw[i]) { wmw[i]=PIN; L1++; } if (mwm[i-1]<=mwm[i]) { mwm[i]=MIN; L2++; } } } // for (int i=1; i<=n; i++) { // cout << mwm[i] << " " << wmw[i] << '\n'; // } cout << min(L1, L2); return 0; } |