#include <iostream> constexpr int maxN = 5e4; constexpr int MAX = 1e9; int n; int a[maxN + 1]; int solve(bool dir) { int last = a[1]; int res = 0; for(int i=2; i<=n; i++) { if(dir) { if(last < a[i]) { last = a[i]; } else { last = MAX; res++; } } else { if(last > a[i]) { last = a[i]; } else { last = -MAX; res++; } } dir = !dir; } return res; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); std::cin >> n; for(int i=1; i<=n; i++) { std::cin >> a[i]; } std::cout << std::min(solve(0), solve(1)); }
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 47 48 49 50 51 | #include <iostream> constexpr int maxN = 5e4; constexpr int MAX = 1e9; int n; int a[maxN + 1]; int solve(bool dir) { int last = a[1]; int res = 0; for(int i=2; i<=n; i++) { if(dir) { if(last < a[i]) { last = a[i]; } else { last = MAX; res++; } } else { if(last > a[i]) { last = a[i]; } else { last = -MAX; res++; } } dir = !dir; } return res; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); std::cin >> n; for(int i=1; i<=n; i++) { std::cin >> a[i]; } std::cout << std::min(solve(0), solve(1)); } |