#include <iostream> #include <algorithm> #include <string> #include <deque> using namespace std; int main() { int n; cin >> n; deque<int> v(n); for (auto &i : v) { cin >> i; } int res = n; auto f = deque<function<bool(int, int)>>{{greater<int>{}, less<int>{}}}; for (int b = 0; b < 2; b++) { int tmp = 0; int cnt = 0; for (int i = 1; i < n; i++) { if (f[(i+b) % 2](v[i], v[i-1])) { tmp += (cnt + 1) / 2; cnt = 0; } else { cnt++; } } tmp += (cnt + 1) / 2; res = min(res, tmp); } cout << res << endl; }
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 | #include <iostream> #include <algorithm> #include <string> #include <deque> using namespace std; int main() { int n; cin >> n; deque<int> v(n); for (auto &i : v) { cin >> i; } int res = n; auto f = deque<function<bool(int, int)>>{{greater<int>{}, less<int>{}}}; for (int b = 0; b < 2; b++) { int tmp = 0; int cnt = 0; for (int i = 1; i < n; i++) { if (f[(i+b) % 2](v[i], v[i-1])) { tmp += (cnt + 1) / 2; cnt = 0; } else { cnt++; } } tmp += (cnt + 1) / 2; res = min(res, tmp); } cout << res << endl; } |