//Wiktor Kotala #include <bits/stdc++.h> #define f first #define s second #define add push_back using namespace std; typedef long long ll; typedef pair<int,int> pint; typedef pair<ll,ll> pll; const int inf = 1'000'000'001; vector<int> V; int solve(){ vector<int> lens; int dlugosc = 0, Vs = V.size(); bool rosnie = 1; for(int i=0; i<Vs-1; i++){ if((rosnie && V[i] < V[i+1]) || (!rosnie && V[i] > V[i+1])){ lens.add(dlugosc); dlugosc = 0; } else{ dlugosc++; } rosnie = !rosnie; } lens.add(dlugosc); int odp = 0; for(const int& len : lens){ odp += (len+1) / 2; } return odp; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, a; cin >> n; for(int i=0; i<n; i++){ cin >> a; V.add(a); } int ans = solve(); V.insert(V.begin(), -inf); cout << min(ans, solve()); 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 | //Wiktor Kotala #include <bits/stdc++.h> #define f first #define s second #define add push_back using namespace std; typedef long long ll; typedef pair<int,int> pint; typedef pair<ll,ll> pll; const int inf = 1'000'000'001; vector<int> V; int solve(){ vector<int> lens; int dlugosc = 0, Vs = V.size(); bool rosnie = 1; for(int i=0; i<Vs-1; i++){ if((rosnie && V[i] < V[i+1]) || (!rosnie && V[i] > V[i+1])){ lens.add(dlugosc); dlugosc = 0; } else{ dlugosc++; } rosnie = !rosnie; } lens.add(dlugosc); int odp = 0; for(const int& len : lens){ odp += (len+1) / 2; } return odp; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, a; cin >> n; for(int i=0; i<n; i++){ cin >> a; V.add(a); } int ans = solve(); V.insert(V.begin(), -inf); cout << min(ans, solve()); return 0; } |