#include <algorithm> #include <cstdint> #include <ios> #include <iostream> #include <iterator> #include <limits> #include <string> #include <vector> template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cerr << s << ": '"; print_value(v); std::cerr << "'\n"; } template <> void print_value(const std::vector<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <typename T> void print_value(const T &v) { std::cerr << v; } void prelude() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); } int main() { prelude(); int n; std::cin >> n; std::vector<int> chirps(n); for (int i = 0; i < n; ++i) { std::cin >> chirps[i]; } // The optimal layout is either /\/\/\/\ or \/\/\/\/, we check both. // In any misaligned const int dirs[] = {-1, 1}; int best = std::numeric_limits<int>::max(); for (int dir : dirs) { int needed_changes = 0; for (int i = 1; i < n; ++i) { int diff = chirps[i] - chirps[i - 1]; if (diff * dir <= 0) { needed_changes += 1; // No change needed on the next step, and we need to skip either way // because we don't update the structure. ++i; dir *= -1; } dir *= -1; } best = std::min(needed_changes, best); } std::cout << best; }
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 53 54 55 56 57 58 59 60 | #include <algorithm> #include <cstdint> #include <ios> #include <iostream> #include <iterator> #include <limits> #include <string> #include <vector> template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cerr << s << ": '"; print_value(v); std::cerr << "'\n"; } template <> void print_value(const std::vector<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <typename T> void print_value(const T &v) { std::cerr << v; } void prelude() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); } int main() { prelude(); int n; std::cin >> n; std::vector<int> chirps(n); for (int i = 0; i < n; ++i) { std::cin >> chirps[i]; } // The optimal layout is either /\/\/\/\ or \/\/\/\/, we check both. // In any misaligned const int dirs[] = {-1, 1}; int best = std::numeric_limits<int>::max(); for (int dir : dirs) { int needed_changes = 0; for (int i = 1; i < n; ++i) { int diff = chirps[i] - chirps[i - 1]; if (diff * dir <= 0) { needed_changes += 1; // No change needed on the next step, and we need to skip either way // because we don't update the structure. ++i; dir *= -1; } dir *= -1; } best = std::min(needed_changes, best); } std::cout << best; } |