#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> v(n); vector<int> w(n); for (int i = 0; i < n; i++) { cin >> v[i]; w[i] = v[i]; } // gora dol gora int a = 0; bool a_ok = true; bool up_down = true; for (int i = 1; i < n; i++) { if (up_down) { if (v[i - 1] <= v[i]) { if (v[i] == -1000000000) { a_ok = false; break; } v[i] = -1000000000; a++; } } else { if (v[i - 1] >= v[i]) { if (v[i] == 1000000000) { a_ok = false; break; } v[i] = 1000000000; a++; } } up_down = !up_down; } // dol gora dol int b = 0; bool b_ok = true; up_down = false; for (int i = 1; i < n; i++) { if (up_down) { if (w[i - 1] <= w[i]) { if (w[i] == -1000000000) { b_ok = false; break; } w[i] = -1000000000; b++; } } else { if (w[i - 1] >= w[i]) { if (w[i] == 1000000000) { b_ok = false; break; } w[i] = 1000000000; b++; } } up_down = !up_down; } if (a_ok && b_ok) cout << min(a, b); else if (a_ok) cout << a; else cout << b; }
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 | #include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> v(n); vector<int> w(n); for (int i = 0; i < n; i++) { cin >> v[i]; w[i] = v[i]; } // gora dol gora int a = 0; bool a_ok = true; bool up_down = true; for (int i = 1; i < n; i++) { if (up_down) { if (v[i - 1] <= v[i]) { if (v[i] == -1000000000) { a_ok = false; break; } v[i] = -1000000000; a++; } } else { if (v[i - 1] >= v[i]) { if (v[i] == 1000000000) { a_ok = false; break; } v[i] = 1000000000; a++; } } up_down = !up_down; } // dol gora dol int b = 0; bool b_ok = true; up_down = false; for (int i = 1; i < n; i++) { if (up_down) { if (w[i - 1] <= w[i]) { if (w[i] == -1000000000) { b_ok = false; break; } w[i] = -1000000000; b++; } } else { if (w[i - 1] >= w[i]) { if (w[i] == 1000000000) { b_ok = false; break; } w[i] = 1000000000; b++; } } up_down = !up_down; } if (a_ok && b_ok) cout << min(a, b); else if (a_ok) cout << a; else cout << b; } |