/* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; int n; LL p[50100]; LL a[50100]; // last leg up, original value LL b[50100]; // last leg down, original value LL c[50100]; // last leg up, changed value - big LL d[50100]; // last leg down, changed value - small /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); cin >> n; FOR(i,1,n) cin >> p[i]; c[2] = 1; d[2] = 1; if (p[2]>p[1]) { a[2]=0; b[2]=1; } else if (p[2]<p[1]) { a[2]=1; b[2]=0; } else { a[2]=1; b[2]=1; }; for (int i=3; i<=n; i++) { a[i] = d[i-1]; if (p[i-1]<p[i]) if (b[i-1] < a[i]) a[i]=b[i-1]; b[i] = c[i-1]; if (p[i-1]>p[i]) if (a[i-1]<b[i]) b[i]=a[i-1]; c[i] = 1 + b[i-1]; if (d[i-1] < b[i-1]) c[i] = 1 + d[i-1]; // not necessary? d[i] = 1 + a[i-1]; if (c[i-1] < a[i-1]) d[i] = 1 + c[i-1]; // not necessary? } LL opt = a[n]; if (b[n] < opt) opt = b[n]; if (c[n] < opt) opt = c[n]; if (d[n] < opt) opt = d[n]; cout << opt << '\n'; 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 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; int n; LL p[50100]; LL a[50100]; // last leg up, original value LL b[50100]; // last leg down, original value LL c[50100]; // last leg up, changed value - big LL d[50100]; // last leg down, changed value - small /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); cin >> n; FOR(i,1,n) cin >> p[i]; c[2] = 1; d[2] = 1; if (p[2]>p[1]) { a[2]=0; b[2]=1; } else if (p[2]<p[1]) { a[2]=1; b[2]=0; } else { a[2]=1; b[2]=1; }; for (int i=3; i<=n; i++) { a[i] = d[i-1]; if (p[i-1]<p[i]) if (b[i-1] < a[i]) a[i]=b[i-1]; b[i] = c[i-1]; if (p[i-1]>p[i]) if (a[i-1]<b[i]) b[i]=a[i-1]; c[i] = 1 + b[i-1]; if (d[i-1] < b[i-1]) c[i] = 1 + d[i-1]; // not necessary? d[i] = 1 + a[i-1]; if (c[i-1] < a[i-1]) d[i] = 1 + c[i-1]; // not necessary? } LL opt = a[n]; if (b[n] < opt) opt = b[n]; if (c[n] < opt) opt = c[n]; if (d[n] < opt) opt = d[n]; cout << opt << '\n'; return 0; } |