#include <bits/stdc++.h> using namespace std; typedef long long LL; int main (){ int n, wyn=0, tab[50010], wyn2=0; cin>>n; for (int i=0; i<n; i++) cin>>tab[i]; for (int i=1; i<n; i++){ if(i%2==0 and tab[i-1]<=tab[i]) wyn++, i++; else if(i%2==1 and tab[i-1]>=tab[i]) wyn++, i++; } for (int i=1; i<n; i++){ if(i%2==0 and tab[i-1]>=tab[i]) wyn2++, i++; else if(i%2==1 and tab[i-1]<=tab[i]) wyn2++, i++; } cout<<min(wyn, wyn2); 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; int main (){ int n, wyn=0, tab[50010], wyn2=0; cin>>n; for (int i=0; i<n; i++) cin>>tab[i]; for (int i=1; i<n; i++){ if(i%2==0 and tab[i-1]<=tab[i]) wyn++, i++; else if(i%2==1 and tab[i-1]>=tab[i]) wyn++, i++; } for (int i=1; i<n; i++){ if(i%2==0 and tab[i-1]>=tab[i]) wyn2++, i++; else if(i%2==1 and tab[i-1]<=tab[i]) wyn2++, i++; } cout<<min(wyn, wyn2); return 0; } |