#include <cstdio> #include <vector> int main () { int n; scanf("%d", &n); --n; int last, i; scanf("%d", &last); std::vector<short> ds; for (i = 0; i < n; ++i) { int x; scanf("%d", &x); short d = x > last ? 1 : x == last ? 0 : -1; ds.push_back(d); last = x; } int r1 = 0; int r2 = 0; i = 0; while (i < n) { if (((i % 2 == 1) && (ds[i] >= 0)) || ((i % 2 == 0) && (ds[i] <= 0))) { ++r1; ++i; } ++i; } i = 0; while (i < n) { if (((i % 2 == 1) && (ds[i] <= 0)) || ((i % 2 == 0) && (ds[i] >= 0))) { ++r2; ++i; } ++i; } printf("%d\n", std::min(r1, r2)); 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 | #include <cstdio> #include <vector> int main () { int n; scanf("%d", &n); --n; int last, i; scanf("%d", &last); std::vector<short> ds; for (i = 0; i < n; ++i) { int x; scanf("%d", &x); short d = x > last ? 1 : x == last ? 0 : -1; ds.push_back(d); last = x; } int r1 = 0; int r2 = 0; i = 0; while (i < n) { if (((i % 2 == 1) && (ds[i] >= 0)) || ((i % 2 == 0) && (ds[i] <= 0))) { ++r1; ++i; } ++i; } i = 0; while (i < n) { if (((i % 2 == 1) && (ds[i] <= 0)) || ((i % 2 == 0) && (ds[i] >= 0))) { ++r2; ++i; } ++i; } printf("%d\n", std::min(r1, r2)); return 0; } |