#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; int n, tab[50005], bat[50005], i; int go(int q) { int res = 0; bat[0] = tab[0]; for (int i=1;i<n;i++) { bool gret = (i % 2 == q); if ((gret && tab[i] > bat[i - 1]) || (!gret && tab[i] < bat[i - 1])) { bat[i] = tab[i]; continue; } if (gret) bat[i] = 10000000; else bat[i] = -10000000; res++; } return res; } int main() { scanf("%d", &n); for (i=0;i<n;i++) scanf("%d", &tab[i]); printf("%d\n", min(go(1), go(0))); 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 <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; int n, tab[50005], bat[50005], i; int go(int q) { int res = 0; bat[0] = tab[0]; for (int i=1;i<n;i++) { bool gret = (i % 2 == q); if ((gret && tab[i] > bat[i - 1]) || (!gret && tab[i] < bat[i - 1])) { bat[i] = tab[i]; continue; } if (gret) bat[i] = 10000000; else bat[i] = -10000000; res++; } return res; } int main() { scanf("%d", &n); for (i=0;i<n;i++) scanf("%d", &tab[i]); printf("%d\n", min(go(1), go(0))); return 0; } |