#include <iostream>
int n;
long long rising[50007];
long long dropping[50007];
long long alt;
const long long stuff = 1 << 20;
int main() {
std::cin.tie(0);
std::cout.tie(0);
std::ios_base::sync_with_stdio(0);
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> alt;
rising[i] = alt;
dropping[i] = alt;
}
// Rising
int rising_min = 0;
for (int i = 1; i < n; i++) {
if (i & 1) {
if (rising[i] <= rising[i-1]) {
rising_min++;
rising[i] = stuff;
}
} else {
if (rising[i] >= rising[i-1]) {
rising_min++;
rising[i] = -stuff;
}
}
}
// Dropping
int dropping_min = 0;
for (int i = 1; i < n; i++) {
if (i & 1) {
if (dropping[i] >= dropping[i-1]) {
dropping_min++;
dropping[i] = -stuff;
}
} else {
if (dropping[i] <= dropping[i-1]) {
dropping_min++;
dropping[i] = stuff;
}
}
}
if (rising_min < dropping_min) {
std::cout << rising_min;
} else {
std::cout << dropping_min;
}
}
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 52 53 54 55 56 57 58 59 60 | #include <iostream> int n; long long rising[50007]; long long dropping[50007]; long long alt; const long long stuff = 1 << 20; int main() { std::cin.tie(0); std::cout.tie(0); std::ios_base::sync_with_stdio(0); std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> alt; rising[i] = alt; dropping[i] = alt; } // Rising int rising_min = 0; for (int i = 1; i < n; i++) { if (i & 1) { if (rising[i] <= rising[i-1]) { rising_min++; rising[i] = stuff; } } else { if (rising[i] >= rising[i-1]) { rising_min++; rising[i] = -stuff; } } } // Dropping int dropping_min = 0; for (int i = 1; i < n; i++) { if (i & 1) { if (dropping[i] >= dropping[i-1]) { dropping_min++; dropping[i] = -stuff; } } else { if (dropping[i] <= dropping[i-1]) { dropping_min++; dropping[i] = stuff; } } } if (rising_min < dropping_min) { std::cout << rising_min; } else { std::cout << dropping_min; } } |
English