#include <bits/stdc++.h> using namespace std; #define endl '\n' #define fi first #define sc second #define LL long long int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; vector<int> Tab(N); for(auto &x : Tab) cin >> x; int Result = 0; bool prevmin = false, prevmax = false, nmin = false, nmax = false; for(int i = 1; i < N; i++){ // zaczynamy do gory if(i % 2 == 1){ if(!prevmin && !prevmax && Tab[i] <= Tab[i-1]){ Result++; nmax = true; } } else{ if(!prevmin && !prevmax && Tab[i] >= Tab[i-1]){ Result++; nmin = true; } } prevmax = nmax; nmax = false; prevmin = nmin; nmin = false; } prevmax = false; prevmin = false; int tempres = 0; for(int i = 1; i < N; i++){ // zaczynamy do gory if(i % 2 == 1){ if(!prevmin && !prevmax && Tab[i] >= Tab[i-1]){ tempres++; nmin = true; } } else{ if(!prevmin && !prevmax && Tab[i] <= Tab[i-1]){ tempres++; nmax = true; } } prevmax = nmax; nmax = false; prevmin = nmin; nmin = false; } Result = min(Result, tempres); cout << Result << 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 | #include <bits/stdc++.h> using namespace std; #define endl '\n' #define fi first #define sc second #define LL long long int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; vector<int> Tab(N); for(auto &x : Tab) cin >> x; int Result = 0; bool prevmin = false, prevmax = false, nmin = false, nmax = false; for(int i = 1; i < N; i++){ // zaczynamy do gory if(i % 2 == 1){ if(!prevmin && !prevmax && Tab[i] <= Tab[i-1]){ Result++; nmax = true; } } else{ if(!prevmin && !prevmax && Tab[i] >= Tab[i-1]){ Result++; nmin = true; } } prevmax = nmax; nmax = false; prevmin = nmin; nmin = false; } prevmax = false; prevmin = false; int tempres = 0; for(int i = 1; i < N; i++){ // zaczynamy do gory if(i % 2 == 1){ if(!prevmin && !prevmax && Tab[i] >= Tab[i-1]){ tempres++; nmin = true; } } else{ if(!prevmin && !prevmax && Tab[i] <= Tab[i-1]){ tempres++; nmax = true; } } prevmax = nmax; nmax = false; prevmin = nmin; nmin = false; } Result = min(Result, tempres); cout << Result << endl; return 0; } |