#include <iostream> #include <vector> using namespace std; int MAX_V = 1000000000; int MIN_V = -MAX_V; int solve(vector<int> &vec) { int cnt_a = 0, cnt_b = 0; int last_a = vec[0], last_b = vec[0]; for(int i = 1; i < vec.size(); i++) { if(i & 1) { if(last_a > vec[i]) { last_a = vec[i]; } else { last_a = MIN_V; cnt_a++; } if(last_b < vec[i]) { last_b = vec[i]; } else { last_b = MAX_V; cnt_b++; } } else { if(last_a < vec[i]) { last_a = vec[i]; } else { last_a = MAX_V; cnt_a++; } if(last_b > vec[i]) { last_b = vec[i]; } else { last_b = MIN_V; cnt_b++; } } } return cnt_a < cnt_b ? cnt_a : cnt_b; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> vec; for(int i = 0; i < n; i++) { int x; cin >> x; vec.push_back(x); } cout << solve(vec) << endl; 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 | #include <iostream> #include <vector> using namespace std; int MAX_V = 1000000000; int MIN_V = -MAX_V; int solve(vector<int> &vec) { int cnt_a = 0, cnt_b = 0; int last_a = vec[0], last_b = vec[0]; for(int i = 1; i < vec.size(); i++) { if(i & 1) { if(last_a > vec[i]) { last_a = vec[i]; } else { last_a = MIN_V; cnt_a++; } if(last_b < vec[i]) { last_b = vec[i]; } else { last_b = MAX_V; cnt_b++; } } else { if(last_a < vec[i]) { last_a = vec[i]; } else { last_a = MAX_V; cnt_a++; } if(last_b > vec[i]) { last_b = vec[i]; } else { last_b = MIN_V; cnt_b++; } } } return cnt_a < cnt_b ? cnt_a : cnt_b; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> vec; for(int i = 0; i < n; i++) { int x; cin >> x; vec.push_back(x); } cout << solve(vec) << endl; return 0; } |