#include <iostream> using namespace std; int n, ai1, ai2, new_ai; int main() { cin >> n; cin >> ai1; ai2 = ai1; int trend = -1, count1 = 0, count2 = 0, count; for (int i = 0; i < n - 1; i++) { cin >> new_ai; if ((new_ai - ai1) * trend <= 0) { count1++; ai1 = trend * 1000001; } else ai1 = new_ai; trend = -trend; if ((new_ai - ai2) * trend <= 0) { count2++; ai2 = trend * 1000001; } else ai2 = new_ai; } count = (count1 <= count2) ? count1 : count2; cout << count; 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 | #include <iostream> using namespace std; int n, ai1, ai2, new_ai; int main() { cin >> n; cin >> ai1; ai2 = ai1; int trend = -1, count1 = 0, count2 = 0, count; for (int i = 0; i < n - 1; i++) { cin >> new_ai; if ((new_ai - ai1) * trend <= 0) { count1++; ai1 = trend * 1000001; } else ai1 = new_ai; trend = -trend; if ((new_ai - ai2) * trend <= 0) { count2++; ai2 = trend * 1000001; } else ai2 = new_ai; } count = (count1 <= count2) ? count1 : count2; cout << count; return 0; } |