// Autor: Adam Szymaszkiewicz #include <bits/stdc++.h> using namespace std; typedef long long LL; LL maxi = 10000000; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<LL> v1(n); vector<LL> v2(n); for (int i = 0; i < n; i++) { cin >> v1[i]; v2[i] = v1[i]; } // 1, -1, 1, -1 int wyn1 = 0; for (int i = 1; i < n; i++) { if (i%2 == 0) { if (v1[i-1] >= v1[i]) { v1[i] = maxi; wyn1++; } } if (i%2 == 1) { if (v1[i-1] <= v1[i]) { v1[i] = -maxi; wyn1++; } } } // -1, 1, -1, 1 int wyn2 = 0; for (int i = 1; i < n; i++) { if (i%2 == 0) { if (v2[i-1] <= v2[i]) { v2[i] = -maxi; wyn2++; } } if (i%2 == 1) { if (v2[i-1] >= v2[i]) { v2[i] = maxi; wyn2++; } } } cout << min(wyn1, wyn2) << "\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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | // Autor: Adam Szymaszkiewicz #include <bits/stdc++.h> using namespace std; typedef long long LL; LL maxi = 10000000; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<LL> v1(n); vector<LL> v2(n); for (int i = 0; i < n; i++) { cin >> v1[i]; v2[i] = v1[i]; } // 1, -1, 1, -1 int wyn1 = 0; for (int i = 1; i < n; i++) { if (i%2 == 0) { if (v1[i-1] >= v1[i]) { v1[i] = maxi; wyn1++; } } if (i%2 == 1) { if (v1[i-1] <= v1[i]) { v1[i] = -maxi; wyn1++; } } } // -1, 1, -1, 1 int wyn2 = 0; for (int i = 1; i < n; i++) { if (i%2 == 0) { if (v2[i-1] <= v2[i]) { v2[i] = -maxi; wyn2++; } } if (i%2 == 1) { if (v2[i-1] >= v2[i]) { v2[i] = maxi; wyn2++; } } } cout << min(wyn1, wyn2) << "\n"; } |