#include <bits/stdc++.h> using namespace std; int main(){ int n; scanf("%d \n",&n); int wynik1 = 0; //np 1 3 1 3 int wynik2 = 0; //np 3 1 3 1 int curr1,curr2,temp; scanf("%d",&temp); curr1 = temp; curr2 = temp; for (int i = 0; i < n - 1;i++){ scanf("%d",&temp); if (i % 2 == 0){//wynik1 sie zwieksza,wynik2 spada if (temp > curr1){ curr1 = temp; } else{ wynik1++; curr1 = 10000001; } /// if (temp < curr2){ curr2 = temp; } else{ wynik2++; curr2 = -10000001; } } else{ if (temp > curr2){ curr2 = temp; } else{ wynik2++; curr2 = 10000001; } /// if (temp < curr1){ curr1 = temp; } else{ wynik1++; curr1 = -10000001; } } } printf("%d",min(wynik1,wynik2)); }
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; int main(){ int n; scanf("%d \n",&n); int wynik1 = 0; //np 1 3 1 3 int wynik2 = 0; //np 3 1 3 1 int curr1,curr2,temp; scanf("%d",&temp); curr1 = temp; curr2 = temp; for (int i = 0; i < n - 1;i++){ scanf("%d",&temp); if (i % 2 == 0){//wynik1 sie zwieksza,wynik2 spada if (temp > curr1){ curr1 = temp; } else{ wynik1++; curr1 = 10000001; } /// if (temp < curr2){ curr2 = temp; } else{ wynik2++; curr2 = -10000001; } } else{ if (temp > curr2){ curr2 = temp; } else{ wynik2++; curr2 = 10000001; } /// if (temp < curr1){ curr1 = temp; } else{ wynik1++; curr1 = -10000001; } } } printf("%d",min(wynik1,wynik2)); } |