#include <iostream> #include <stdio.h> using namespace std; int signOf(int value) { return value > 0 ? 1 : value < 0 ? -1 : 0; } int main() { int n; cin >> n; int tones[n]; for (int i = 0; i < n; i++) { int tone; cin >> tone; tones[i] = tone; } int signs[n]; for (int i = 0; i < n - 1; i++) { signs[i] = signOf(tones[i + 1] - tones[i]); } int downErrors = 0; for (int i = 0; i < n - 1; i++) { int expectSign = (i % 2) * 2 - 1; if (signs[i] != expectSign) { downErrors++; i++; } } int upErrors = 0; for (int i = 0; i < n - 1; i++) { int expectSign = -((i % 2) * 2 - 1); if (signs[i] != expectSign) { upErrors++; i++; } } printf("%d", upErrors < downErrors ? upErrors : downErrors); 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 | #include <iostream> #include <stdio.h> using namespace std; int signOf(int value) { return value > 0 ? 1 : value < 0 ? -1 : 0; } int main() { int n; cin >> n; int tones[n]; for (int i = 0; i < n; i++) { int tone; cin >> tone; tones[i] = tone; } int signs[n]; for (int i = 0; i < n - 1; i++) { signs[i] = signOf(tones[i + 1] - tones[i]); } int downErrors = 0; for (int i = 0; i < n - 1; i++) { int expectSign = (i % 2) * 2 - 1; if (signs[i] != expectSign) { downErrors++; i++; } } int upErrors = 0; for (int i = 0; i < n - 1; i++) { int expectSign = -((i % 2) * 2 - 1); if (signs[i] != expectSign) { upErrors++; i++; } } printf("%d", upErrors < downErrors ? upErrors : downErrors); return 0; } |