#include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) vector<int> numbers; int go(int start) { int low = 1, high = 1, org = 0; FOR(i,1,SZ(numbers)) { int down = (i % 2 == start); int low2 = INF, high2 = INF, org2 = INF; if (down) { low2 = MIN(high + 1, org + 1); org2 = high; if (numbers[i] < numbers[i - 1]) org2 = MIN(org2, org); } else { high2 = MIN(low + 1, org + 1); org2 = low; if (numbers[i] > numbers[i - 1]) org2 = MIN(org2, org); } low = low2; high = high2; org = org2; } return MIN(MIN(low, high), org); } int main() { int n; scanf("%d", &n); FOR(i,0,n) { int a; scanf("%d", &a); numbers.push_back(a); } printf("%d\n", MIN(go(0), go(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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) vector<int> numbers; int go(int start) { int low = 1, high = 1, org = 0; FOR(i,1,SZ(numbers)) { int down = (i % 2 == start); int low2 = INF, high2 = INF, org2 = INF; if (down) { low2 = MIN(high + 1, org + 1); org2 = high; if (numbers[i] < numbers[i - 1]) org2 = MIN(org2, org); } else { high2 = MIN(low + 1, org + 1); org2 = low; if (numbers[i] > numbers[i - 1]) org2 = MIN(org2, org); } low = low2; high = high2; org = org2; } return MIN(MIN(low, high), org); } int main() { int n; scanf("%d", &n); FOR(i,0,n) { int a; scanf("%d", &a); numbers.push_back(a); } printf("%d\n", MIN(go(0), go(1))); } |