#include<bits/stdc++.h> using namespace std; const int duzo = 50007; int tab[duzo]; int tab1[duzo]; int tab2[duzo]; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++){ cin >> tab[i]; tab1[i] = tab[i]; tab2[i] = tab[i]; } //duze male duze // 0 1 2 int wynik1=0; for (int i = 1; i < n; i++){ if (i%2==0 and tab1[i] <= tab1[i-1]){ wynik1++; tab1[i]=1000000007; } else if (i%2==1 and tab1[i] >= tab1[i-1]){ wynik1++; tab1[i]=-1000000007; } } //male duze male // 0 1 2 int wynik2=0; for (int i = 1; i < n; i++){ if (i%2==0 and tab2[i] >= tab2[i-1]){ wynik2++; tab2[i]=-1000000007; } else if (i%2==1 and tab2[i] <= tab2[i-1]){ wynik2++; tab2[i]=1000000007; } } cout << min(wynik1, wynik2); //male duze male }
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; const int duzo = 50007; int tab[duzo]; int tab1[duzo]; int tab2[duzo]; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++){ cin >> tab[i]; tab1[i] = tab[i]; tab2[i] = tab[i]; } //duze male duze // 0 1 2 int wynik1=0; for (int i = 1; i < n; i++){ if (i%2==0 and tab1[i] <= tab1[i-1]){ wynik1++; tab1[i]=1000000007; } else if (i%2==1 and tab1[i] >= tab1[i-1]){ wynik1++; tab1[i]=-1000000007; } } //male duze male // 0 1 2 int wynik2=0; for (int i = 1; i < n; i++){ if (i%2==0 and tab2[i] >= tab2[i-1]){ wynik2++; tab2[i]=-1000000007; } else if (i%2==1 and tab2[i] <= tab2[i-1]){ wynik2++; tab2[i]=1000000007; } } cout << min(wynik1, wynik2); //male duze male } |