#include <iostream> using namespace std; constexpr int N = 50004, MIN = -1e9, MAX = 1e9; int n, tab[N], tab2[N], out, out2; bool rise = true, rise2 = true; int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n; for (int i = 0; i < n; i++){ cin>>tab[i]; tab2[i] = tab[i]; } for (int i = 0; i < n - 1; i++) { if(rise){ if(tab[i] <= tab[i+1]){ out++; tab[i+1] = MIN; } rise = false; }else{ if(tab[i] >= tab[i+1]){ out++; tab[i+1] = MAX; } rise = true; } if(rise2){ if(tab2[i] >= tab2[i+1]){ out2++; tab2[i+1] = MAX; } rise2 = false; }else{ if(tab2[i] <= tab2[i+1]){ out2++; tab2[i+1] = MIN; } rise2 = true; } } cout<<min(out, out2)<<"\n"; 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 | #include <iostream> using namespace std; constexpr int N = 50004, MIN = -1e9, MAX = 1e9; int n, tab[N], tab2[N], out, out2; bool rise = true, rise2 = true; int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n; for (int i = 0; i < n; i++){ cin>>tab[i]; tab2[i] = tab[i]; } for (int i = 0; i < n - 1; i++) { if(rise){ if(tab[i] <= tab[i+1]){ out++; tab[i+1] = MIN; } rise = false; }else{ if(tab[i] >= tab[i+1]){ out++; tab[i+1] = MAX; } rise = true; } if(rise2){ if(tab2[i] >= tab2[i+1]){ out2++; tab2[i+1] = MAX; } rise2 = false; }else{ if(tab2[i] <= tab2[i+1]){ out2++; tab2[i+1] = MIN; } rise2 = true; } } cout<<min(out, out2)<<"\n"; return 0; } |