#include <iostream> #include <vector> using namespace std; int MAX = 100000000; inline int sgn(int x) { return (x > 0) ? 1 : (x == 0 ? 0 : -1); } int uporzadkuj(int delta, int A[], int n) // jesli delta = +1, to oczekujemy, ze dwa elementy sa w porzadku rosnacym: A[i] < A[i+1] // jesli delta = -1, to oczekujemy, ze dwa elementy sa w porzadku malejacym: A[i] > A[i+1] { int lzmian = 0; // cout << "przebieg " << delta << endl; for (int i = 1; i < n; i++) { if (sgn(A[i] - A[i - 1]) != delta) { ++lzmian; // cout << delta << " --- " << i << ": A[i] = " << A[i] << ", A[i-1] = " << A[i - 1] << endl; A[i] = delta * MAX; } delta = -delta; } return lzmian; } int main() { int n; scanf("%d", &n); int A[n], B[n]; int el; // wczytaj ciag for (int i = 0; i < n; i++) { scanf("%d", &el); A[i] = el; B[i] = el; } int k1 = uporzadkuj(1, A, n); int k2 = uporzadkuj(-1, B, n); printf("%d\n", min(k1, k2)); 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 51 52 | #include <iostream> #include <vector> using namespace std; int MAX = 100000000; inline int sgn(int x) { return (x > 0) ? 1 : (x == 0 ? 0 : -1); } int uporzadkuj(int delta, int A[], int n) // jesli delta = +1, to oczekujemy, ze dwa elementy sa w porzadku rosnacym: A[i] < A[i+1] // jesli delta = -1, to oczekujemy, ze dwa elementy sa w porzadku malejacym: A[i] > A[i+1] { int lzmian = 0; // cout << "przebieg " << delta << endl; for (int i = 1; i < n; i++) { if (sgn(A[i] - A[i - 1]) != delta) { ++lzmian; // cout << delta << " --- " << i << ": A[i] = " << A[i] << ", A[i-1] = " << A[i - 1] << endl; A[i] = delta * MAX; } delta = -delta; } return lzmian; } int main() { int n; scanf("%d", &n); int A[n], B[n]; int el; // wczytaj ciag for (int i = 0; i < n; i++) { scanf("%d", &el); A[i] = el; B[i] = el; } int k1 = uporzadkuj(1, A, n); int k2 = uporzadkuj(-1, B, n); printf("%d\n", min(k1, k2)); return 0; } |