#include <iostream> int n; int tab[50001]; int counter[2]; int last[2]; int MAX_VAL = 1e9; int MIN_VAL = -1e9; using namespace std; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> tab[i]; } last[0] = tab[1]; last[1] = tab[1]; for (int i = 2; i <= n; i++) { counter[i % 2] += (tab[i] > last[i % 2] ? 0 : 1); last[i % 2] = (tab[i] > last[i % 2] ? tab[i] : MAX_VAL); counter[(i+1) % 2] += (tab[i] < last[(i+1) % 2] ? 0 : 1); last[(i+1) % 2] = (tab[i] < last[(i+1) % 2] ? tab[i] : MIN_VAL); } cout << min(counter[0], counter[1]) << endl; 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 | #include <iostream> int n; int tab[50001]; int counter[2]; int last[2]; int MAX_VAL = 1e9; int MIN_VAL = -1e9; using namespace std; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> tab[i]; } last[0] = tab[1]; last[1] = tab[1]; for (int i = 2; i <= n; i++) { counter[i % 2] += (tab[i] > last[i % 2] ? 0 : 1); last[i % 2] = (tab[i] > last[i % 2] ? tab[i] : MAX_VAL); counter[(i+1) % 2] += (tab[i] < last[(i+1) % 2] ? 0 : 1); last[(i+1) % 2] = (tab[i] < last[(i+1) % 2] ? tab[i] : MIN_VAL); } cout << min(counter[0], counter[1]) << endl; return 0; } |