#include <iostream> #include <algorithm> #include <vector> using namespace std; std::vector<int> v; int n; int changes(bool up, bool dummy) { int res = 0; if (dummy) res++; for (int i=1;i<n;i++) { up = !up; if (dummy==true) { dummy = false; continue; } if (up && v[i-1] < v[i]) continue; if (!up && v[i-1] > v[i]) continue; dummy = true; res++; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; v.resize(n); for (int i=0;i<n;i++) cin >> v[i]; int res = std::min(n, changes(true, false)); res = std::min(res, changes(false,false)); if (v.size()%2) { res = std::min(res, changes(false,true)); res = std::min(res, changes(true,true)); } std::cout << res << std::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 40 41 42 43 44 45 46 | #include <iostream> #include <algorithm> #include <vector> using namespace std; std::vector<int> v; int n; int changes(bool up, bool dummy) { int res = 0; if (dummy) res++; for (int i=1;i<n;i++) { up = !up; if (dummy==true) { dummy = false; continue; } if (up && v[i-1] < v[i]) continue; if (!up && v[i-1] > v[i]) continue; dummy = true; res++; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n; v.resize(n); for (int i=0;i<n;i++) cin >> v[i]; int res = std::min(n, changes(true, false)); res = std::min(res, changes(false,false)); if (v.size()%2) { res = std::min(res, changes(false,true)); res = std::min(res, changes(true,true)); } std::cout << res << std::endl; } |