#ifndef LOCAL #pragma GCC optimize("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) { sim > struct rge {c b, e; }; sim > rge<c> range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") int MAX = 1000 * 1000 * 1000; int check(vector<int> v, bool dir) { int res = 0; for (int i = 0; i + 1 < v.size(); i ++) { if (dir && v[i] >= v[i+1]) { v[i+1] = MAX; res ++; } if (!dir && v[i] <= v[i+1]) { v[i+1] = -MAX; res ++; } dir = !dir; } return res; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } cout << min(check(v, true), check(v, false)); }
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 | #ifndef LOCAL #pragma GCC optimize("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) { sim > struct rge {c b, e; }; sim > rge<c> range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") int MAX = 1000 * 1000 * 1000; int check(vector<int> v, bool dir) { int res = 0; for (int i = 0; i + 1 < v.size(); i ++) { if (dir && v[i] >= v[i+1]) { v[i+1] = MAX; res ++; } if (!dir && v[i] <= v[i+1]) { v[i+1] = -MAX; res ++; } dir = !dir; } return res; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } cout << min(check(v, true), check(v, false)); } |