#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int t[n], l1=0, l2=0;cin>>t[0]; bool pr1=false, pr2=false; for(int i=1;i<n;i++){ cin>>t[i]; if(pr1){ pr1=false; }else if(i%2==0&&t[i-1]>=t[i]){ l1++; pr1=true; }else if(i%2==1&&t[i-1]<=t[i]){ l1++; pr1=true; } if(pr2){ pr2=false; }else if(i%2==0&&t[i-1]<=t[i]){ l2++; pr2=true; }else if(i%2==1&&t[i-1]>=t[i]){ l2++; pr2=true; } } if(l1<l2)cout<<l1; else cout<<l2; 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 | #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int t[n], l1=0, l2=0;cin>>t[0]; bool pr1=false, pr2=false; for(int i=1;i<n;i++){ cin>>t[i]; if(pr1){ pr1=false; }else if(i%2==0&&t[i-1]>=t[i]){ l1++; pr1=true; }else if(i%2==1&&t[i-1]<=t[i]){ l1++; pr1=true; } if(pr2){ pr2=false; }else if(i%2==0&&t[i-1]<=t[i]){ l2++; pr2=true; }else if(i%2==1&&t[i-1]>=t[i]){ l2++; pr2=true; } } if(l1<l2)cout<<l1; else cout<<l2; return 0; } |