#include <iostream> using namespace std; int T[100001], TW[100001]; int main() { int n, w = 1e9, w0 = 0, INF = 1e9; bool g = 0; cin>>n; for (int i = 1; i <= n; i ++) cin>>T[i]; TW[1] = T[1]; for (int i = 2; i <= n; i ++) //wzrasta-spada ^v { TW[i] = T[i]; if (i % 2 == 0 && TW[i - 1] >= T[i]) { TW[i] = INF; w0 ++; } else if (i % 2 == 1 && TW[i - 1] <= T[i]) { TW[i] = -INF; w0 ++; } } w = min(w, w0); w0 = 0; for (int i = 2; i <= n; i ++) //spada-wzrasta v^ { TW[i] = T[i]; if (i % 2 == 0 && TW[i - 1] <= T[i]) { TW[i] = -INF; w0 ++; } else if (i % 2 == 1 && TW[i - 1] >= T[i]) { TW[i] = INF; w0 ++; } } w = min(w, w0); w0 = 0; cout<<w<<'\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 | #include <iostream> using namespace std; int T[100001], TW[100001]; int main() { int n, w = 1e9, w0 = 0, INF = 1e9; bool g = 0; cin>>n; for (int i = 1; i <= n; i ++) cin>>T[i]; TW[1] = T[1]; for (int i = 2; i <= n; i ++) //wzrasta-spada ^v { TW[i] = T[i]; if (i % 2 == 0 && TW[i - 1] >= T[i]) { TW[i] = INF; w0 ++; } else if (i % 2 == 1 && TW[i - 1] <= T[i]) { TW[i] = -INF; w0 ++; } } w = min(w, w0); w0 = 0; for (int i = 2; i <= n; i ++) //spada-wzrasta v^ { TW[i] = T[i]; if (i % 2 == 0 && TW[i - 1] <= T[i]) { TW[i] = -INF; w0 ++; } else if (i % 2 == 1 && TW[i - 1] >= T[i]) { TW[i] = INF; w0 ++; } } w = min(w, w0); w0 = 0; cout<<w<<'\n'; } |