#include <iostream>
int64_t n;
int64_t a[50002];
int64_t p[50002][2];
int64_t inf = 1000000000;
int64_t find(bool high) {
p[0][0] = 0;
p[0][1] = 1;
for (decltype(n) i = 1; i < n; i++, high = !high) {
p[i][0] = p[i-1][1];
if (high && a[i] > a[i-1]) {
p[i][0] = std::min(p[i][0], p[i-1][0]);
}
if (!high && a[i] < a[i-1]) {
p[i][0] = std::min(p[i][0], p[i-1][0]);
}
p[i][1] = std::min(p[i-1][0], p[i-1][1]) + 1;
}
return std::min(p[n-1][0], p[n-1][1]);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin >> n;
for (decltype(n) i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << std::min(find(true), find(false)) << "\n";
}
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 | #include <iostream> int64_t n; int64_t a[50002]; int64_t p[50002][2]; int64_t inf = 1000000000; int64_t find(bool high) { p[0][0] = 0; p[0][1] = 1; for (decltype(n) i = 1; i < n; i++, high = !high) { p[i][0] = p[i-1][1]; if (high && a[i] > a[i-1]) { p[i][0] = std::min(p[i][0], p[i-1][0]); } if (!high && a[i] < a[i-1]) { p[i][0] = std::min(p[i][0], p[i-1][0]); } p[i][1] = std::min(p[i-1][0], p[i-1][1]) + 1; } return std::min(p[n-1][0], p[n-1][1]); } int main() { std::ios_base::sync_with_stdio(false); std::cin >> n; for (decltype(n) i = 0; i < n; i++) { std::cin >> a[i]; } std::cout << std::min(find(true), find(false)) << "\n"; } |
English