#include <bits/stdc++.h> using namespace std; const int MAXX = 1e5+5; const int INF = 1e9+5; int tab[MAXX]; int pom[MAXX]; int main() { int n; cin >> n; for (int i=0; i<n; i++) cin >> tab[i], pom[i] = tab[i]; int res1=0, res2=0; for (int i=1; i<n; i++) { if (i % 2 == 0 && tab[i] <= tab[i-1]) tab[i] = INF, res1++; if (i % 2 == 1 && tab[i] >= tab[i-1]) tab[i] = -INF, res1++; } for (int i=1; i<n; i++) { if (i % 2 == 0 && pom[i] >= pom[i-1]) pom[i] = -INF, res2++; if (i % 2 == 1 && pom[i] <= pom[i-1]) pom[i] = INF, res2++; } cout << min(res1, res2); 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 | #include <bits/stdc++.h> using namespace std; const int MAXX = 1e5+5; const int INF = 1e9+5; int tab[MAXX]; int pom[MAXX]; int main() { int n; cin >> n; for (int i=0; i<n; i++) cin >> tab[i], pom[i] = tab[i]; int res1=0, res2=0; for (int i=1; i<n; i++) { if (i % 2 == 0 && tab[i] <= tab[i-1]) tab[i] = INF, res1++; if (i % 2 == 1 && tab[i] >= tab[i-1]) tab[i] = -INF, res1++; } for (int i=1; i<n; i++) { if (i % 2 == 0 && pom[i] >= pom[i-1]) pom[i] = -INF, res2++; if (i % 2 == 1 && pom[i] <= pom[i-1]) pom[i] = INF, res2++; } cout << min(res1, res2); return 0; } |