#include <iostream> #include <climits> using std::cin, std::cout; const char endl = '\n'; using ln = long long; //!!! temp 50 const int MAX_N = 50'007; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int hievenchange = 0; int hioddchange = 0; int prevhiodd = INT_MAX; int prevhieven = -INT_MAX; for (int i = 0; i < n; i++) { int x; cin >> x; if (i%2==0) { //in hieven, x has to be higher //in hiodd, x has to be lower //than previously if (x > prevhieven) prevhieven = x; else { hievenchange++; prevhieven = INT_MAX; } if (x < prevhiodd) prevhiodd = x; else { hioddchange++; prevhiodd = -INT_MAX; } } else { if (x > prevhiodd) prevhiodd = x; else { hioddchange++; prevhiodd = INT_MAX; } if (x < prevhieven) prevhieven = x; else { hievenchange++; prevhieven = -INT_MAX; } } //cout << prevhieven << ' ' << prevhiodd << '\n'; } cout << std::min(hievenchange, hioddchange); }
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 <iostream> #include <climits> using std::cin, std::cout; const char endl = '\n'; using ln = long long; //!!! temp 50 const int MAX_N = 50'007; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int hievenchange = 0; int hioddchange = 0; int prevhiodd = INT_MAX; int prevhieven = -INT_MAX; for (int i = 0; i < n; i++) { int x; cin >> x; if (i%2==0) { //in hieven, x has to be higher //in hiodd, x has to be lower //than previously if (x > prevhieven) prevhieven = x; else { hievenchange++; prevhieven = INT_MAX; } if (x < prevhiodd) prevhiodd = x; else { hioddchange++; prevhiodd = -INT_MAX; } } else { if (x > prevhiodd) prevhiodd = x; else { hioddchange++; prevhiodd = INT_MAX; } if (x < prevhieven) prevhieven = x; else { hievenchange++; prevhieven = -INT_MAX; } } //cout << prevhieven << ' ' << prevhiodd << '\n'; } cout << std::min(hievenchange, hioddchange); } |