#include <iostream> int kk = 1000001; int n; int main() { std::ios_base::sync_with_stdio(0); std::cin >> n; int dir = 1; int sum1 = 0; int sum2 = 0; int wp1; std::cin >> wp1; int wp2 = wp1; for (int i=1;i<n;++i) { int w; std::cin >> w; if ((w-wp1)*dir <= 0) { ++sum1; wp1 = dir*kk; } else wp1 = w; if ((wp2-w)*dir <= 0) { ++sum2; wp2 = -dir*kk; } else wp2 = w; dir = -dir; } std::cout << std::min(sum1, sum2) << std::endl; 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 | #include <iostream> int kk = 1000001; int n; int main() { std::ios_base::sync_with_stdio(0); std::cin >> n; int dir = 1; int sum1 = 0; int sum2 = 0; int wp1; std::cin >> wp1; int wp2 = wp1; for (int i=1;i<n;++i) { int w; std::cin >> w; if ((w-wp1)*dir <= 0) { ++sum1; wp1 = dir*kk; } else wp1 = w; if ((wp2-w)*dir <= 0) { ++sum2; wp2 = -dir*kk; } else wp2 = w; dir = -dir; } std::cout << std::min(sum1, sum2) << std::endl; return 0; } |