#include <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <string> #include <vector> using namespace std; const int INF = 1e9 + 1; int f(deque<int> a, bool is_min) { int res = 0; a.push_front(is_min ? INF : -INF); for (int i = 1; i < a.size(); i++) { if (is_min && !(a[i] < a[i - 1])) { res++; a[i] = -(INF + i); } else if (!is_min && !(a[i] > a[i - 1])) { res++; a[i] = (INF + i); } is_min = !is_min; } return res; } int solve() { int n; cin >> n; deque<int> a(n); a.resize(n); for (auto &x : a) cin >> x; return min(f(a, true), f(a, false)); } int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); cout << solve() << "\n"; }
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 | #include <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <string> #include <vector> using namespace std; const int INF = 1e9 + 1; int f(deque<int> a, bool is_min) { int res = 0; a.push_front(is_min ? INF : -INF); for (int i = 1; i < a.size(); i++) { if (is_min && !(a[i] < a[i - 1])) { res++; a[i] = -(INF + i); } else if (!is_min && !(a[i] > a[i - 1])) { res++; a[i] = (INF + i); } is_min = !is_min; } return res; } int solve() { int n; cin >> n; deque<int> a(n); a.resize(n); for (auto &x : a) cin >> x; return min(f(a, true), f(a, false)); } int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); cout << solve() << "\n"; } |