#include <iostream> using namespace std; int check(bool isFirstHigh, int*& sounds, int len) { int out = 0; bool isHigh = isFirstHigh; int last = ((isFirstHigh) ? -1e7 : 1e7); for (int i = 0; i < len; i++) { if (isHigh) { if (sounds[i] <= last) { out++; last = 1e7; }else last = sounds[i]; } else { if (sounds[i] >= last) { out++; last = -1e7; } else last = sounds[i]; } isHigh = !isHigh; } return out; } int main() { int n; cin >> n; int* sounds = new int[n]; for (int i = 0; i < n; i++) { cin >> sounds[i]; } int tmp1 = check(1, sounds, n); int tmp2 = check(0, sounds, n); if (tmp2 < tmp1) tmp1 = tmp2; cout << tmp1; 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 49 50 51 | #include <iostream> using namespace std; int check(bool isFirstHigh, int*& sounds, int len) { int out = 0; bool isHigh = isFirstHigh; int last = ((isFirstHigh) ? -1e7 : 1e7); for (int i = 0; i < len; i++) { if (isHigh) { if (sounds[i] <= last) { out++; last = 1e7; }else last = sounds[i]; } else { if (sounds[i] >= last) { out++; last = -1e7; } else last = sounds[i]; } isHigh = !isHigh; } return out; } int main() { int n; cin >> n; int* sounds = new int[n]; for (int i = 0; i < n; i++) { cin >> sounds[i]; } int tmp1 = check(1, sounds, n); int tmp2 = check(0, sounds, n); if (tmp2 < tmp1) tmp1 = tmp2; cout << tmp1; return 0; } |