#include <iostream> const int MAX_N = 50000; int N; int A[50001]; int LiczZmiany(bool up) { int Zmiany = 0; int Previous = A[0]; for (int i = 1; i < N; i++) { int Current = A[i]; int Next = A[i+1]; if (up && Current <= Previous) { Zmiany++; Current = std::max(Previous+1, Next+1); } else if (!up && Current >= Previous) { Zmiany++; Current = std::min(Previous-1, Next-1); } up = !up; Previous = Current; } return Zmiany; } int main() { std::cin >> N; for (int i = 0; i < N; i++) std::cin >> A[i]; A[N] = A[N+1]; int B = LiczZmiany(true); int C = LiczZmiany(false); int Wynik = std::min(B, C); std::cout << Wynik << "\n"; return 0; }
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 | #include <iostream> const int MAX_N = 50000; int N; int A[50001]; int LiczZmiany(bool up) { int Zmiany = 0; int Previous = A[0]; for (int i = 1; i < N; i++) { int Current = A[i]; int Next = A[i+1]; if (up && Current <= Previous) { Zmiany++; Current = std::max(Previous+1, Next+1); } else if (!up && Current >= Previous) { Zmiany++; Current = std::min(Previous-1, Next-1); } up = !up; Previous = Current; } return Zmiany; } int main() { std::cin >> N; for (int i = 0; i < N; i++) std::cin >> A[i]; A[N] = A[N+1]; int B = LiczZmiany(true); int C = LiczZmiany(false); int Wynik = std::min(B, C); std::cout << Wynik << "\n"; return 0; } |