#include <iostream>
#include <vector>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::vector<int> data;
std::cin >> n;
for (int i=0; i<n; i++) {
int val;
std::cin >> val;
data.push_back(val);
}
int lowResult = 0;
int lowLast = data[0];
int highResult = 0;
int highLast = data[0];
for (int i=1; i<n; i++) {
int val = data.at(i);
if (i % 2 == 1) {
if (lowLast >= val) {
lowResult++;
lowLast = 10000000;
} else {
lowLast = val;
}
if (highLast <= val) {
highResult++;
highLast = -10000000;
} else {
highLast = val;
}
} else {
if (highLast >= val) {
highResult++;
highLast = 10000000;
} else {
highLast = val;
}
if (lowLast <= val) {
lowResult++;
lowLast = -10000000;
} else {
lowLast = val;
}
}
}
std::cout<< std::min(highResult, lowResult) << "\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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <iostream> #include <vector> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; std::vector<int> data; std::cin >> n; for (int i=0; i<n; i++) { int val; std::cin >> val; data.push_back(val); } int lowResult = 0; int lowLast = data[0]; int highResult = 0; int highLast = data[0]; for (int i=1; i<n; i++) { int val = data.at(i); if (i % 2 == 1) { if (lowLast >= val) { lowResult++; lowLast = 10000000; } else { lowLast = val; } if (highLast <= val) { highResult++; highLast = -10000000; } else { highLast = val; } } else { if (highLast >= val) { highResult++; highLast = 10000000; } else { highLast = val; } if (lowLast <= val) { lowResult++; lowLast = -10000000; } else { lowLast = val; } } } std::cout<< std::min(highResult, lowResult) << "\n"; } |
English