#include <cstdio> #include <vector> using namespace std; constexpr int INF = 1e9; int n; vector<int> a, b; int solve(vector<int> &arr, int rem) { int cnt = 0; for (int i = 1; i < n; ++i) { if (i%2 == rem && arr[i-1] >= arr[i]) { arr[i] = INF; ++cnt; } if (i%2 != rem && arr[i-1] <= arr[i]) { arr[i] = -INF; ++cnt; } } return cnt; } int main(void) { scanf("%d", &n); a.resize(n); b.resize(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); b[i] = a[i]; } int result = min( solve(a, 0), solve(b, 1)); printf("%d\n", result); 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 39 40 | #include <cstdio> #include <vector> using namespace std; constexpr int INF = 1e9; int n; vector<int> a, b; int solve(vector<int> &arr, int rem) { int cnt = 0; for (int i = 1; i < n; ++i) { if (i%2 == rem && arr[i-1] >= arr[i]) { arr[i] = INF; ++cnt; } if (i%2 != rem && arr[i-1] <= arr[i]) { arr[i] = -INF; ++cnt; } } return cnt; } int main(void) { scanf("%d", &n); a.resize(n); b.resize(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); b[i] = a[i]; } int result = min( solve(a, 0), solve(b, 1)); printf("%d\n", result); return 0; } |