#include <bits/stdc++.h> using namespace std; int main () { int n; cin>>n; int up=0, down=0, prevup, prevdn; int num; cin>>num; prevup=num; prevdn=num; for (int i = 1; i < n;i++) { cin>>num; if(num>=prevup && i%2==0) { up++; prevup=1000001; } else if(num<=prevup&&i%2==1) { up++; prevup=-1000001; } else { prevup=num; } if(num<=prevdn && i%2==0) { down++; prevdn=1000001; } else if (num>=prevdn && i%2==1) { down++; prevdn=-1000001; } else { prevdn=num; } } cout<<min(up,down)<<endl; }
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 | #include <bits/stdc++.h> using namespace std; int main () { int n; cin>>n; int up=0, down=0, prevup, prevdn; int num; cin>>num; prevup=num; prevdn=num; for (int i = 1; i < n;i++) { cin>>num; if(num>=prevup && i%2==0) { up++; prevup=1000001; } else if(num<=prevup&&i%2==1) { up++; prevup=-1000001; } else { prevup=num; } if(num<=prevdn && i%2==0) { down++; prevdn=1000001; } else if (num>=prevdn && i%2==1) { down++; prevdn=-1000001; } else { prevdn=num; } } cout<<min(up,down)<<endl; } |