#include <iostream> using namespace std; int n; int prev_decreased = -1000000000, prev_increased = 1000000000; int res_decreased = 0, res_increased = 0; int cur; int cur_decreased, cur_increased; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 0; i < n; ++i){ swap(res_decreased, res_increased); cin >> cur; if(cur < prev_increased){ cur_decreased = cur; } else{ cur_decreased = -1000000000; res_decreased++; } if(cur > prev_decreased){ cur_increased = cur; } else{ cur_increased = 1000000000; res_increased++; } prev_increased = cur_increased; prev_decreased = cur_decreased; } cout << min(res_decreased, res_increased) << endl; }
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 | #include <iostream> using namespace std; int n; int prev_decreased = -1000000000, prev_increased = 1000000000; int res_decreased = 0, res_increased = 0; int cur; int cur_decreased, cur_increased; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 0; i < n; ++i){ swap(res_decreased, res_increased); cin >> cur; if(cur < prev_increased){ cur_decreased = cur; } else{ cur_decreased = -1000000000; res_decreased++; } if(cur > prev_decreased){ cur_increased = cur; } else{ cur_increased = 1000000000; res_increased++; } prev_increased = cur_increased; prev_decreased = cur_decreased; } cout << min(res_decreased, res_increased) << endl; } |