#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; const int MAX_N = 5*1e4 + 10; int a[MAX_N]; int f[MAX_N][2][3]; const int MAX_V = 1e9; const int MIN_V = -1e9; const int UP = 0; const int DOWN = 1; const int DEC = 0; const int ORG = 1; const int INC = 2; int main() { //ios_base::sync_with_stdio(0); int n; scanf("%d", &n); //cin >> n; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); //cin >> a[i]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 3; ++k) { f[i][j][k] = INF; } } } if (a[0] < a[1]) { f[1][UP][ORG] = 0; f[1][UP][INC] = 1; f[1][DOWN][ORG] = 1; f[1][DOWN][DEC] = 1; } else if (a[0] > a[1]) { f[1][UP][ORG] = 1; f[1][UP][INC] = 1; f[1][DOWN][ORG] = 0; f[1][DOWN][DEC] = 1; } else { f[1][UP][ORG] = 1; f[1][UP][INC] = 1; f[1][DOWN][ORG] = 1; f[1][DOWN][DEC] = 1; } for (int i = 2; i < n; ++i) { f[i][UP][INC] = min(f[i-1][DOWN][ORG], f[i-1][DOWN][DEC]) + 1; f[i][DOWN][DEC] = min(f[i-1][UP][ORG], f[i-1][UP][INC]) + 1; f[i][UP][ORG] = f[i-1][DOWN][DEC]; if (a[i-1] < a[i]) { f[i][UP][ORG] = min(f[i][UP][ORG], f[i-1][DOWN][ORG]); } f[i][DOWN][ORG] = f[i-1][UP][INC]; if (a[i-1] > a[i]) { f[i][DOWN][ORG] = min(f[i][DOWN][ORG], f[i-1][UP][ORG]); } } int res = INF; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { res = min(res, f[n-1][i][j]); } } printf("%d\n", res); //cout << res << endl; 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; const int MAX_N = 5*1e4 + 10; int a[MAX_N]; int f[MAX_N][2][3]; const int MAX_V = 1e9; const int MIN_V = -1e9; const int UP = 0; const int DOWN = 1; const int DEC = 0; const int ORG = 1; const int INC = 2; int main() { //ios_base::sync_with_stdio(0); int n; scanf("%d", &n); //cin >> n; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); //cin >> a[i]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 3; ++k) { f[i][j][k] = INF; } } } if (a[0] < a[1]) { f[1][UP][ORG] = 0; f[1][UP][INC] = 1; f[1][DOWN][ORG] = 1; f[1][DOWN][DEC] = 1; } else if (a[0] > a[1]) { f[1][UP][ORG] = 1; f[1][UP][INC] = 1; f[1][DOWN][ORG] = 0; f[1][DOWN][DEC] = 1; } else { f[1][UP][ORG] = 1; f[1][UP][INC] = 1; f[1][DOWN][ORG] = 1; f[1][DOWN][DEC] = 1; } for (int i = 2; i < n; ++i) { f[i][UP][INC] = min(f[i-1][DOWN][ORG], f[i-1][DOWN][DEC]) + 1; f[i][DOWN][DEC] = min(f[i-1][UP][ORG], f[i-1][UP][INC]) + 1; f[i][UP][ORG] = f[i-1][DOWN][DEC]; if (a[i-1] < a[i]) { f[i][UP][ORG] = min(f[i][UP][ORG], f[i-1][DOWN][ORG]); } f[i][DOWN][ORG] = f[i-1][UP][INC]; if (a[i-1] > a[i]) { f[i][DOWN][ORG] = min(f[i][DOWN][ORG], f[i-1][UP][ORG]); } } int res = INF; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { res = min(res, f[n-1][i][j]); } } printf("%d\n", res); //cout << res << endl; return 0; } |