#include <iostream>
using namespace std;
int main() {
long n;
long a, a1, a2;
bool up{true};
long res1{}, res2{};
cin >> n >> a;
a1 = a2 = a;
for (int i=1; i<n; i++) {
cin >> a;
if (up) {
if (a > a1) {
a1 = a;
} else {
a1 = 99999999;
res1++;
}
if (a < a2) {
a2 = a;
} else {
a2 = -99999999;
res2++;
}
} else {
if (a < a1) {
a1 = a;
} else {
a1 = -99999999;
res1++;
}
if (a > a2) {
a2 = a;
} else {
a2 = 99999999;
res2++;
}
}
up = !up;
}
cout << min(res1,res2) << 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <iostream> using namespace std; int main() { long n; long a, a1, a2; bool up{true}; long res1{}, res2{}; cin >> n >> a; a1 = a2 = a; for (int i=1; i<n; i++) { cin >> a; if (up) { if (a > a1) { a1 = a; } else { a1 = 99999999; res1++; } if (a < a2) { a2 = a; } else { a2 = -99999999; res2++; } } else { if (a < a1) { a1 = a; } else { a1 = -99999999; res1++; } if (a > a2) { a2 = a; } else { a2 = 99999999; res2++; } } up = !up; } cout << min(res1,res2) << endl; return 0; } |
English