#include <bits/stdc++.h> using namespace std; int t[50001]; int t2[50001]; int n, z, r, w, m, w2; int main() { cin >> n; for (int i=1;i<=n;i++) { cin >> t[i]; t2[i]=t[i]; } z=2; if(t[1]==t[2]) { t[2]=(1<<20); t2[2]=-(1<<20); w++; w2++; } else { if(t[1]<t[2]) {w2++; t2[2]=-(1<<20);} else {w++; t[2]=(1<<20);} } for (int i=3;i<=n;i++) { if(t[i-1]>=t[i] && z%2==1) {w++; t[i]=(1<<20);} if(t[i-1]<=t[i] && z%2==0) {w++; t[i]=-(1<<20);} if(t2[i-1]<=t2[i] && z%2==1) {w2++; t2[i]=-(1<<20);} if(t2[i-1]>=t2[i] && z%2==0) {w2++; t2[i]=(1<<20);} z++; } cout << min(w, w2); }
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 | #include <bits/stdc++.h> using namespace std; int t[50001]; int t2[50001]; int n, z, r, w, m, w2; int main() { cin >> n; for (int i=1;i<=n;i++) { cin >> t[i]; t2[i]=t[i]; } z=2; if(t[1]==t[2]) { t[2]=(1<<20); t2[2]=-(1<<20); w++; w2++; } else { if(t[1]<t[2]) {w2++; t2[2]=-(1<<20);} else {w++; t[2]=(1<<20);} } for (int i=3;i<=n;i++) { if(t[i-1]>=t[i] && z%2==1) {w++; t[i]=(1<<20);} if(t[i-1]<=t[i] && z%2==0) {w++; t[i]=-(1<<20);} if(t2[i-1]<=t2[i] && z%2==1) {w2++; t2[i]=-(1<<20);} if(t2[i-1]>=t2[i] && z%2==0) {w2++; t2[i]=(1<<20);} z++; } cout << min(w, w2); } |