#include <cstdio> #include <cassert> #include <cstring> #include <algorithm> using namespace std; const int max_size = 50005; int n; int t[max_size]; int calctab[3][max_size]; void calc() { for(int i = n-2; i >= 0; --i) { int dir = -1; for(int j = 0; j < 2; ++j) { dir *= -1; if (t[i]*dir < t[i+1]*dir) { calctab[dir+1][i] = calctab[-dir+1][i+1]; } else { calctab[dir+1][i] = 1 + min( calctab[-dir+1][i+1], calctab[dir+1][i+2] ); } } } } int main() { scanf("%d\n", &n); for(int i = 0; i < n; ++i) { scanf("%d", t+i); } calc(); printf("%d\n", min(calctab[0][0], calctab[2][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 32 33 34 35 36 37 38 39 40 41 | #include <cstdio> #include <cassert> #include <cstring> #include <algorithm> using namespace std; const int max_size = 50005; int n; int t[max_size]; int calctab[3][max_size]; void calc() { for(int i = n-2; i >= 0; --i) { int dir = -1; for(int j = 0; j < 2; ++j) { dir *= -1; if (t[i]*dir < t[i+1]*dir) { calctab[dir+1][i] = calctab[-dir+1][i+1]; } else { calctab[dir+1][i] = 1 + min( calctab[-dir+1][i+1], calctab[dir+1][i+2] ); } } } } int main() { scanf("%d\n", &n); for(int i = 0; i < n; ++i) { scanf("%d", t+i); } calc(); printf("%d\n", min(calctab[0][0], calctab[2][0])); return 0; } |