#include <bits/stdc++.h> using namespace std; #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define F first #define S second #define pb push_back #define eb emplace_back #define mkp make_pair #define pque priority_queue #define vt vector #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define END(x) { cout << x; return; } template<int D, typename T> struct vtd : public vector<vtd<D - 1, T>> { template<typename... Args> vtd(int n = 0, Args... args) : vector<vtd<D - 1, T>>(n, vtd<D - 1, T>(args...)) {} }; template<typename T> struct vtd<1, T> : public vector<T> { vtd(int n = 0, const T& val = T()) : vector<T>(n, val) {} }; #define PI (ld)3.1415926535897932384626433832795 #define MOD (int)1000000007 #define inf (int)1000000000 #define INF (ll)1000000000000000001 #define EPS 1e-9 typedef long double ld; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair <int, int> pii; typedef pair <ll, ll> pll; #ifdef NOT_OJ #define debug(x) cerr << #x << ": "; __print(x); cerr << "\n"; #else #define debug(x) #endif //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void __print(ll t) { cerr << t; } void __print(int t) { cerr << t; } void __print(string t) { cerr << t; } void __print(char t) { cerr << t; } void __print(ld t) { cerr << t; } void __print(double t) { cerr << t; } void __print(ull t) { cerr << t; } void __print(uint t) { cerr << t; } void __print(bool t) { cerr << t; } template <class T, class V> void __print(pair <T, V> p); template <class T> void __print(vt <T> v); template <class T> void __print(set <T> v); template <class T, class V> void __print(map <T, V> v); template <class T> void __print(multiset <T> v); template <class T> void __print(vt <vt <T>> v); template <int D, class T> void __print(vt <vtd <D, T>> v); template <class T> void __print(unordered_set <T> v); template <class T, class V> void __print(unordered_map <T, V> v); template <class T, class V> void __print(pair <T, V> p) {cerr << "{"; __print(p.F); cerr << ","; __print(p.S); cerr << "}";} template <class T> void __print(vt <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(vt <vt <T>> v) {cerr << "\n"; for (int i = 0; i < sz(v); i++) {cerr << " "<< i << ": "; __print(v[i]); cerr << "\n";}} template <int D, class T> void __print(vt <vtd <D, T>> v) {cerr << "\n"; for (int i = 0; i < sz(v); i++) {cerr << " "<< i << ": "; __print(v[i]); cerr << "\n";}} template <class T> void __print(set <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(unordered_set <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(multiset <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T, class V> void __print(map <T, V> v) {cerr << "[ "; for (auto i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T, class V> void __print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {__print(i); cerr << " ";} cerr << "]";} int solve(vt <int> &v, bool at) { int ans = 0, prev = v[0]; for (int i = 1; i < sz(v); i++) { int x = v[i]; if (at) { // prev > x if (prev <= x) { ans++; prev = -inf; } else prev = x; } else { // prev < x if (prev >= x) { ans++; prev = inf; } else prev = x; } at = 1 - at; } return ans; } void __runcase() { int n; cin >> n; vt <int> v(n); for (auto &x : v) cin >> x; cout << min(solve(v, 0), solve(v, 1)) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int __test_cases = 1; // cin >> __test_cases; while(__test_cases--) { __runcase(); } }
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <bits/stdc++.h> using namespace std; #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define F first #define S second #define pb push_back #define eb emplace_back #define mkp make_pair #define pque priority_queue #define vt vector #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define END(x) { cout << x; return; } template<int D, typename T> struct vtd : public vector<vtd<D - 1, T>> { template<typename... Args> vtd(int n = 0, Args... args) : vector<vtd<D - 1, T>>(n, vtd<D - 1, T>(args...)) {} }; template<typename T> struct vtd<1, T> : public vector<T> { vtd(int n = 0, const T& val = T()) : vector<T>(n, val) {} }; #define PI (ld)3.1415926535897932384626433832795 #define MOD (int)1000000007 #define inf (int)1000000000 #define INF (ll)1000000000000000001 #define EPS 1e-9 typedef long double ld; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair <int, int> pii; typedef pair <ll, ll> pll; #ifdef NOT_OJ #define debug(x) cerr << #x << ": "; __print(x); cerr << "\n"; #else #define debug(x) #endif //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void __print(ll t) { cerr << t; } void __print(int t) { cerr << t; } void __print(string t) { cerr << t; } void __print(char t) { cerr << t; } void __print(ld t) { cerr << t; } void __print(double t) { cerr << t; } void __print(ull t) { cerr << t; } void __print(uint t) { cerr << t; } void __print(bool t) { cerr << t; } template <class T, class V> void __print(pair <T, V> p); template <class T> void __print(vt <T> v); template <class T> void __print(set <T> v); template <class T, class V> void __print(map <T, V> v); template <class T> void __print(multiset <T> v); template <class T> void __print(vt <vt <T>> v); template <int D, class T> void __print(vt <vtd <D, T>> v); template <class T> void __print(unordered_set <T> v); template <class T, class V> void __print(unordered_map <T, V> v); template <class T, class V> void __print(pair <T, V> p) {cerr << "{"; __print(p.F); cerr << ","; __print(p.S); cerr << "}";} template <class T> void __print(vt <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(vt <vt <T>> v) {cerr << "\n"; for (int i = 0; i < sz(v); i++) {cerr << " "<< i << ": "; __print(v[i]); cerr << "\n";}} template <int D, class T> void __print(vt <vtd <D, T>> v) {cerr << "\n"; for (int i = 0; i < sz(v); i++) {cerr << " "<< i << ": "; __print(v[i]); cerr << "\n";}} template <class T> void __print(set <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(unordered_set <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T> void __print(multiset <T> v) {cerr << "[ "; for (T i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T, class V> void __print(map <T, V> v) {cerr << "[ "; for (auto i : v) {__print(i); cerr << " ";} cerr << "]";} template <class T, class V> void __print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {__print(i); cerr << " ";} cerr << "]";} int solve(vt <int> &v, bool at) { int ans = 0, prev = v[0]; for (int i = 1; i < sz(v); i++) { int x = v[i]; if (at) { // prev > x if (prev <= x) { ans++; prev = -inf; } else prev = x; } else { // prev < x if (prev >= x) { ans++; prev = inf; } else prev = x; } at = 1 - at; } return ans; } void __runcase() { int n; cin >> n; vt <int> v(n); for (auto &x : v) cin >> x; cout << min(solve(v, 0), solve(v, 1)) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int __test_cases = 1; // cin >> __test_cases; while(__test_cases--) { __runcase(); } } |