#include <bits/stdc++.h> using namespace std; int n; int tab[500005]; int dp[2][500005]; bool ch[2][500005]; int main() { scanf("%d", &n); for(int i = 0; i < n; ++i) { scanf("%d", tab+i); } //0 - upper //1 - lower dp[0][0] = 0; dp[1][0] = 0; if(tab[0] == tab[1]) { dp[0][1] = 1; ch[0][1] = 1; dp[1][1] = 1; ch[1][1] = 1; } else if(tab[0] < tab[1]) { dp[0][1] = 0; dp[1][1] = 1; ch[1][1] = 1; } else { dp[0][1] = 1; ch[0][1] = 1; dp[1][1] = 0; } for(int i = 2; i < n; ++i) { dp[0][i] = dp[1][i-1] + (ch[1][i-1] ? 0 : (tab[i-1] >= tab[i] ? 1 : 0)); dp[1][i] = dp[0][i-1] + (ch[0][i-1] ? 0 : (tab[i-1] <= tab[i] ? 1 : 0)); ch[0][i] = (ch[1][i-1] ? 0 : (tab[i-1] >= tab[i] ? 1 : 0)); ch[1][i] = (ch[0][i-1] ? 0 : (tab[i-1] <= tab[i] ? 1 : 0)); } /*for(int i = 0; i < n; ++i) { printf("%d %d %d %d %d\n", i, dp[0][i], dp[1][i], ch[0][i], ch[1][i]); }*/ printf("%d\n", min(dp[0][n-1], dp[1][n-1])); }
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 | #include <bits/stdc++.h> using namespace std; int n; int tab[500005]; int dp[2][500005]; bool ch[2][500005]; int main() { scanf("%d", &n); for(int i = 0; i < n; ++i) { scanf("%d", tab+i); } //0 - upper //1 - lower dp[0][0] = 0; dp[1][0] = 0; if(tab[0] == tab[1]) { dp[0][1] = 1; ch[0][1] = 1; dp[1][1] = 1; ch[1][1] = 1; } else if(tab[0] < tab[1]) { dp[0][1] = 0; dp[1][1] = 1; ch[1][1] = 1; } else { dp[0][1] = 1; ch[0][1] = 1; dp[1][1] = 0; } for(int i = 2; i < n; ++i) { dp[0][i] = dp[1][i-1] + (ch[1][i-1] ? 0 : (tab[i-1] >= tab[i] ? 1 : 0)); dp[1][i] = dp[0][i-1] + (ch[0][i-1] ? 0 : (tab[i-1] <= tab[i] ? 1 : 0)); ch[0][i] = (ch[1][i-1] ? 0 : (tab[i-1] >= tab[i] ? 1 : 0)); ch[1][i] = (ch[0][i-1] ? 0 : (tab[i-1] <= tab[i] ? 1 : 0)); } /*for(int i = 0; i < n; ++i) { printf("%d %d %d %d %d\n", i, dp[0][i], dp[1][i], ch[0][i], ch[1][i]); }*/ printf("%d\n", min(dp[0][n-1], dp[1][n-1])); } |