#include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int main() { boost; int n; cin >> n; int res = INT_MAX; // always assume + - + - + - vi vec(n); rep(i, 0, n) cin >> vec[i]; rep(step, 0, 2) { vi changed(n, INT_MAX), eq(n, INT_MAX); changed[0] = 1; eq[0] = 0; rep(i, 1, n) { // if this one is changed, it's always possible changed[i] = 1 + min(changed[i - 1], eq[i - 1]); bool should_be_smaller = i % 2; bool should_be_larger = !should_be_smaller; eq[i] = changed[i - 1]; //not changing always possible if (should_be_smaller && vec[i] < vec[i - 1]) { eq[i] = min(eq[i], eq[i - 1]); } if (should_be_larger && vec[i] > vec[i - 1]) { eq[i] = min(eq[i], eq[i - 1]); } } res = min(res, min(changed[n - 1], eq[n - 1])); rep(i, 0, n) vec[i] *= -1; } cout << res << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int main() { boost; int n; cin >> n; int res = INT_MAX; // always assume + - + - + - vi vec(n); rep(i, 0, n) cin >> vec[i]; rep(step, 0, 2) { vi changed(n, INT_MAX), eq(n, INT_MAX); changed[0] = 1; eq[0] = 0; rep(i, 1, n) { // if this one is changed, it's always possible changed[i] = 1 + min(changed[i - 1], eq[i - 1]); bool should_be_smaller = i % 2; bool should_be_larger = !should_be_smaller; eq[i] = changed[i - 1]; //not changing always possible if (should_be_smaller && vec[i] < vec[i - 1]) { eq[i] = min(eq[i], eq[i - 1]); } if (should_be_larger && vec[i] > vec[i - 1]) { eq[i] = min(eq[i], eq[i - 1]); } } res = min(res, min(changed[n - 1], eq[n - 1])); rep(i, 0, n) vec[i] *= -1; } cout << res << "\n"; } |