#include<bits/stdc++.h> using namespace std; const int MAXX = 1e9; const int MINX = -1e9; int le(vector<int> data) { int res = 0, n = data.size(); for (int i = 1;i < n;i += 2) { if (data[i] <= data[i - 1]) { ++res; data[i] = MAXX; } else if (i + 1 < n && data[i] <= data[i + 1]) { data[i + 1] = MINX; ++res; } } return res; } int gr(vector<int> data) { int res = 0, n = data.size(); for (int i = 1;i < n;i += 2) { if (data[i] >= data[i - 1]) { ++res; data[i] = MINX; } else if (i + 1 < n && data[i] >= data[i + 1]) { data[i + 1] = MAXX; ++res; } } return res; } void solve() { int n; cin >> n; vector<int> data(n); for (int& d : data) cin >> d; cout << min(le(data), gr(data)) << "\n"; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(nullptr); solve(); }
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 | #include<bits/stdc++.h> using namespace std; const int MAXX = 1e9; const int MINX = -1e9; int le(vector<int> data) { int res = 0, n = data.size(); for (int i = 1;i < n;i += 2) { if (data[i] <= data[i - 1]) { ++res; data[i] = MAXX; } else if (i + 1 < n && data[i] <= data[i + 1]) { data[i + 1] = MINX; ++res; } } return res; } int gr(vector<int> data) { int res = 0, n = data.size(); for (int i = 1;i < n;i += 2) { if (data[i] >= data[i - 1]) { ++res; data[i] = MINX; } else if (i + 1 < n && data[i] >= data[i + 1]) { data[i + 1] = MAXX; ++res; } } return res; } void solve() { int n; cin >> n; vector<int> data(n); for (int& d : data) cin >> d; cout << min(le(data), gr(data)) << "\n"; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(nullptr); solve(); } |