#include <bits/stdc++.h> using namespace std; vector<int> vec; int Solve(); int main() { int sz; cin >> sz; vec = vector<int>(sz); for (int i = 0; i < sz; i++) { cin >> vec[i]; } cout << Solve() << endl; return 0; } int Solve() { int answer = 0; if (vec.size() < 3) { return answer; } vector<int> tmp = vec; bool smaller_now = true; int res1 = 0; for (int i = 1; i < vec.size(); i++) { if (smaller_now && tmp[i - 1] <= tmp[i]) { tmp[i] = INT_MIN; res1++; } else if (!smaller_now && tmp[i - 1] >= tmp[i]) { tmp[i] = INT_MAX; res1++; } smaller_now = !smaller_now; } tmp = vec; smaller_now = false; int res2 = 0; for (int i = 1; i < vec.size(); i++) { if (smaller_now && tmp[i - 1] <= tmp[i]) { tmp[i] = INT_MIN; res2++; } else if (!smaller_now && tmp[i - 1] >= tmp[i]) { tmp[i] = INT_MAX; res2++; } smaller_now = !smaller_now; } return min(res1, res2); }
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 | #include <bits/stdc++.h> using namespace std; vector<int> vec; int Solve(); int main() { int sz; cin >> sz; vec = vector<int>(sz); for (int i = 0; i < sz; i++) { cin >> vec[i]; } cout << Solve() << endl; return 0; } int Solve() { int answer = 0; if (vec.size() < 3) { return answer; } vector<int> tmp = vec; bool smaller_now = true; int res1 = 0; for (int i = 1; i < vec.size(); i++) { if (smaller_now && tmp[i - 1] <= tmp[i]) { tmp[i] = INT_MIN; res1++; } else if (!smaller_now && tmp[i - 1] >= tmp[i]) { tmp[i] = INT_MAX; res1++; } smaller_now = !smaller_now; } tmp = vec; smaller_now = false; int res2 = 0; for (int i = 1; i < vec.size(); i++) { if (smaller_now && tmp[i - 1] <= tmp[i]) { tmp[i] = INT_MIN; res2++; } else if (!smaller_now && tmp[i - 1] >= tmp[i]) { tmp[i] = INT_MAX; res2++; } smaller_now = !smaller_now; } return min(res1, res2); } |