#include <cstdio> #include <algorithm> int comp(int a, int b, int par, int expPar) { if (a <= b && par == expPar) return 1; if (b <= a && par != expPar) return 1; return 0; } int main() { int n; scanf("%d\n", &n); int changes[2] = {0, 0}; bool skip[2] = {false, false}; int prevVal, val; scanf("%d", &val); for (int i=1; i<n; i++) { prevVal = val; scanf("%d", &val); for (int k=0; k<2; k++) { if (skip[k]) skip[k] = false; else if (comp(prevVal, val, i % 2, k)) { changes[k]++; skip[k] = true; } } } printf("%d", std::min(changes[0], changes[1])); }
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 | #include <cstdio> #include <algorithm> int comp(int a, int b, int par, int expPar) { if (a <= b && par == expPar) return 1; if (b <= a && par != expPar) return 1; return 0; } int main() { int n; scanf("%d\n", &n); int changes[2] = {0, 0}; bool skip[2] = {false, false}; int prevVal, val; scanf("%d", &val); for (int i=1; i<n; i++) { prevVal = val; scanf("%d", &val); for (int k=0; k<2; k++) { if (skip[k]) skip[k] = false; else if (comp(prevVal, val, i % 2, k)) { changes[k]++; skip[k] = true; } } } printf("%d", std::min(changes[0], changes[1])); } |