#include <iostream> #include <climits> using namespace std; const int MAXN = 5e4+1; const int INF = 1e9; int a[MAXN], b[MAXN]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i=1; i<=n; i++){ cin >> a[i]; b[i] = a[i];} // 1 przypadek : a[1] > a[2] int ans1 = 0; bool r = true; if (a[1] <= a[2]){ ans1++; a[2] = -INF;} for (int i=3; i<=n; i++){ if (r){ if (a[i] <= a[i-1]){ ans1++; a[i] = INF;} r = false; } else{ if (a[i] >= a[i-1]){ ans1++; a[i] = -INF;} r = true; } } // 2 przypadek: b[2] > b[1] int ans2 = 0; r = false; if (b[2] <= b[1]){ ans2++; b[2] = INF;} for (int i=3; i<=n; i++){ if (r){ if (b[i] <= b[i-1]){ ans2++; b[i] = INF;} r = false; } else{ if (b[i] >= b[i-1]){ ans2++; b[i] = -INF;} r = true; } } cout << min(ans1, ans2) << "\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 | #include <iostream> #include <climits> using namespace std; const int MAXN = 5e4+1; const int INF = 1e9; int a[MAXN], b[MAXN]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i=1; i<=n; i++){ cin >> a[i]; b[i] = a[i];} // 1 przypadek : a[1] > a[2] int ans1 = 0; bool r = true; if (a[1] <= a[2]){ ans1++; a[2] = -INF;} for (int i=3; i<=n; i++){ if (r){ if (a[i] <= a[i-1]){ ans1++; a[i] = INF;} r = false; } else{ if (a[i] >= a[i-1]){ ans1++; a[i] = -INF;} r = true; } } // 2 przypadek: b[2] > b[1] int ans2 = 0; r = false; if (b[2] <= b[1]){ ans2++; b[2] = INF;} for (int i=3; i<=n; i++){ if (r){ if (b[i] <= b[i-1]){ ans2++; b[i] = INF;} r = false; } else{ if (b[i] >= b[i-1]){ ans2++; b[i] = -INF;} r = true; } } cout << min(ans1, ans2) << "\n"; } |