#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; int signum(int a) { return (a > 0) - (a < 0); } int count_result(vi &a, int first, int second) { vi res(a.size()); res[0] = 0; res[1] = signum(a[1] - a[0]) == first ? 0 : 1; swap(first, second); for (size_t i = 2; i < res.size(); ++i) { if (signum(a[i] - a[i - 1]) == first) { res[i] = res[i - 1]; } else { res[i] = res[i - 2] + 1; } swap(first, second); } // for (size_t i = 0; i < res.size(); ++i) { // cerr << res[i] << ' '; // } // cerr << '\n'; return res.back(); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } // cerr << count_result(a, 1, -1) << '\n'; // cerr << count_result(a, -1, 1) << '\n'; cout << min(count_result(a, 1, -1), count_result(a, -1, 1)) << 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 49 50 51 52 53 54 55 56 57 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; int signum(int a) { return (a > 0) - (a < 0); } int count_result(vi &a, int first, int second) { vi res(a.size()); res[0] = 0; res[1] = signum(a[1] - a[0]) == first ? 0 : 1; swap(first, second); for (size_t i = 2; i < res.size(); ++i) { if (signum(a[i] - a[i - 1]) == first) { res[i] = res[i - 1]; } else { res[i] = res[i - 2] + 1; } swap(first, second); } // for (size_t i = 0; i < res.size(); ++i) { // cerr << res[i] << ' '; // } // cerr << '\n'; return res.back(); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vi a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } // cerr << count_result(a, 1, -1) << '\n'; // cerr << count_result(a, -1, 1) << '\n'; cout << min(count_result(a, 1, -1), count_result(a, -1, 1)) << endl; return 0; } |