#include <cstdio> #include <iostream> #include <algorithm> #include <vector> using namespace std; int count_changes(int &n, vector<int> a, int direction) { int changes = 0; for (int i=1; i<n; i++) { if ((a[i] - a[i - 1]) * direction <= 0) { a[i] = 10000000 * direction; changes++; } direction *= -1; } return changes; } int main() { int n; vector<int> a; scanf("%d", &n); a.resize(n); for (int i=0; i<n; i++) { scanf("%d", &a[i]); } printf("%d\n", min(count_changes(n, a, 1), count_changes(n, a, -1))); 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 | #include <cstdio> #include <iostream> #include <algorithm> #include <vector> using namespace std; int count_changes(int &n, vector<int> a, int direction) { int changes = 0; for (int i=1; i<n; i++) { if ((a[i] - a[i - 1]) * direction <= 0) { a[i] = 10000000 * direction; changes++; } direction *= -1; } return changes; } int main() { int n; vector<int> a; scanf("%d", &n); a.resize(n); for (int i=0; i<n; i++) { scanf("%d", &a[i]); } printf("%d\n", min(count_changes(n, a, 1), count_changes(n, a, -1))); return 0; }; |