#include <iostream> #include <vector> int calculate(std::vector<int> & v, bool kind, int n) { int max = 1e9, min = -1e9, res = 0; for (int i = 1; i < n; i++) { if ((v[i] - v[i - 1] >= 0) && (i % 2 == kind)) { v[i] = min; min ++; res ++; } else if ((v[i] - v[i - 1] <= 0) && (i % 2 == (!kind))) { v[i] = max; max --; res ++; } } return res; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); int n; std::cin >> n; std::vector<int> v1(n), v2(n); for (int i = 0; i < n; i++) { std::cin >> v1[i]; v2[i] = v1[i]; } int res = std::min(calculate(v1, true, n), calculate(v2, false, n)); std::cout << res << "\n"; }
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 | #include <iostream> #include <vector> int calculate(std::vector<int> & v, bool kind, int n) { int max = 1e9, min = -1e9, res = 0; for (int i = 1; i < n; i++) { if ((v[i] - v[i - 1] >= 0) && (i % 2 == kind)) { v[i] = min; min ++; res ++; } else if ((v[i] - v[i - 1] <= 0) && (i % 2 == (!kind))) { v[i] = max; max --; res ++; } } return res; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); int n; std::cin >> n; std::vector<int> v1(n), v2(n); for (int i = 0; i < n; i++) { std::cin >> v1[i]; v2[i] = v1[i]; } int res = std::min(calculate(v1, true, n), calculate(v2, false, n)); std::cout << res << "\n"; } |