#include <bits/stdc++.h> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef LOCAL #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif #define ar array #define ll long long #define ld long double #define sza(x) ((int)x.size()) #define all(a) (a).begin(), (a).end() const int MAX_N = 1e5 + 5; const ll MOD = 1e9 + 7; const ll INF = 1e9; const ld EPS = 1e-9; int solve(int *arr, int n){ unsigned int counter1 = 0; unsigned int counter2 = 0; unsigned int i =0; while(i<n-1){ if(i%2==0){ bool flag = false; while(arr[i]>=arr[i+1]){ //cout << i << " " << arr[i] << " " << arr[i+1] << "\n"; counter1++; if(i+2>=n-1){ flag = true; break; } i+=2; } if(flag) break; if(i+1>=n-1)break; i++; } else{ bool flag = false; while(arr[i]<=arr[i+1]){ //cout << i << " " << arr[i] << " " << arr[i+1] << "\n"; counter1++; if(i+2>=n-1){ flag = true; break; } i+=2; } if(flag) break; if(i+1>=n-1)break; i++; } } //cout << counter1 << "\n"; i=0; while(i<n-1){ if(i%2==0){ bool flag = false; while(arr[i]<=arr[i+1]){ //cout << i << " " << arr[i] << " " << arr[i+1] << "\n"; counter2++; if(i+2>=n-1){ flag = true; break; } i+=2; } if(flag) break; if(i+1>=n-1)break; i++; } else{ bool flag = false; while(arr[i]>=arr[i+1]){ //cout << i << " " << arr[i] << " " << arr[i+1] << "\n"; counter2++; if(i+2>=n-1){ flag = true; break; } i+=2; } if(flag) break; if(i+1>=n-1)break; i++; } } //cout << counter1 << " " << counter2 << "\n"; return min(counter1,counter2); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; int n; // cin >> tc; for (int t = 1; t <= tc; t++) { cin >> n; int notes[n]; for(int i=0; i<n; i++){ cin >> notes[i]; } cout << solve(notes,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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include <bits/stdc++.h> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef LOCAL #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif #define ar array #define ll long long #define ld long double #define sza(x) ((int)x.size()) #define all(a) (a).begin(), (a).end() const int MAX_N = 1e5 + 5; const ll MOD = 1e9 + 7; const ll INF = 1e9; const ld EPS = 1e-9; int solve(int *arr, int n){ unsigned int counter1 = 0; unsigned int counter2 = 0; unsigned int i =0; while(i<n-1){ if(i%2==0){ bool flag = false; while(arr[i]>=arr[i+1]){ //cout << i << " " << arr[i] << " " << arr[i+1] << "\n"; counter1++; if(i+2>=n-1){ flag = true; break; } i+=2; } if(flag) break; if(i+1>=n-1)break; i++; } else{ bool flag = false; while(arr[i]<=arr[i+1]){ //cout << i << " " << arr[i] << " " << arr[i+1] << "\n"; counter1++; if(i+2>=n-1){ flag = true; break; } i+=2; } if(flag) break; if(i+1>=n-1)break; i++; } } //cout << counter1 << "\n"; i=0; while(i<n-1){ if(i%2==0){ bool flag = false; while(arr[i]<=arr[i+1]){ //cout << i << " " << arr[i] << " " << arr[i+1] << "\n"; counter2++; if(i+2>=n-1){ flag = true; break; } i+=2; } if(flag) break; if(i+1>=n-1)break; i++; } else{ bool flag = false; while(arr[i]>=arr[i+1]){ //cout << i << " " << arr[i] << " " << arr[i+1] << "\n"; counter2++; if(i+2>=n-1){ flag = true; break; } i+=2; } if(flag) break; if(i+1>=n-1)break; i++; } } //cout << counter1 << " " << counter2 << "\n"; return min(counter1,counter2); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; int n; // cin >> tc; for (int t = 1; t <= tc; t++) { cin >> n; int notes[n]; for(int i=0; i<n; i++){ cin >> notes[i]; } cout << solve(notes,n); } return 0; } |