//#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-all-loops") //#pragma GCC target("avx,sse2,sse3,ssse3,sse4,lzcnt,popcnt") #include <bits/stdc++.h> #define ll long long #define point pair<int, int> #define X first #define Y second #define all(x) (x).begin(), (x).end() #define pb push_back #define show(x) cerr << (#x) << " = " << x << '\n' #define print(x) if (1) {cerr << (#x) << " = "; for (auto it : x) \ cerr << it << ' '; cerr << '\n';} #define fast_io ios_base::sync_with_stdio(0), cin.tie(0) using namespace std; const int N = 200000 + 64; const int INF = 2e9 + 64; const ll MAX = 2e18 + 64; const int MOD = 998244353; int solve(vector<int> v, bool mode) { int res = 0; for (int i = 1; i < v.size(); ++i, mode ^= 1) { if (mode && v[i] >= v[i - 1]) v[i] = -1e9, ++res; else if (!mode && v[i] <= v[i - 1]) v[i] = 1e9, ++res; } return res; } signed main() { fast_io; int n; cin >> n; vector<int> a(n); for (auto& it : a) cin >> it; cout << min(solve(a, 0), solve(a, 1)) << '\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 | //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-all-loops") //#pragma GCC target("avx,sse2,sse3,ssse3,sse4,lzcnt,popcnt") #include <bits/stdc++.h> #define ll long long #define point pair<int, int> #define X first #define Y second #define all(x) (x).begin(), (x).end() #define pb push_back #define show(x) cerr << (#x) << " = " << x << '\n' #define print(x) if (1) {cerr << (#x) << " = "; for (auto it : x) \ cerr << it << ' '; cerr << '\n';} #define fast_io ios_base::sync_with_stdio(0), cin.tie(0) using namespace std; const int N = 200000 + 64; const int INF = 2e9 + 64; const ll MAX = 2e18 + 64; const int MOD = 998244353; int solve(vector<int> v, bool mode) { int res = 0; for (int i = 1; i < v.size(); ++i, mode ^= 1) { if (mode && v[i] >= v[i - 1]) v[i] = -1e9, ++res; else if (!mode && v[i] <= v[i - 1]) v[i] = 1e9, ++res; } return res; } signed main() { fast_io; int n; cin >> n; vector<int> a(n); for (auto& it : a) cin >> it; cout << min(solve(a, 0), solve(a, 1)) << '\n'; } |