#include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; vector<int> values(n, 0); vector<int> changes(n-1, 0); for(int i=0; i<n; i++) cin>>values[i]; for(int i=0; i<n-1; i++){ if(values[i+1]>values[i]) changes[i] = 1; else if(values[i+1]<values[i]) changes[i] = 0; else changes[i] = 2; } // first case int first_result = 0; for(int i=0; i<n-1; i++){ if(i%2==0){ if(changes[i]!=0){ first_result++; i++; } } else{ if(changes[i]!=1){ first_result++; i++; } } } // second case int second_result = 0; for(int i=0; i<n-1; i++){ if(i%2==0){ if(changes[i]!=1){ second_result++; i++; } } else{ if(changes[i]!=0){ second_result++; i++; } } } // output cout<<min(first_result, second_result); 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; vector<int> values(n, 0); vector<int> changes(n-1, 0); for(int i=0; i<n; i++) cin>>values[i]; for(int i=0; i<n-1; i++){ if(values[i+1]>values[i]) changes[i] = 1; else if(values[i+1]<values[i]) changes[i] = 0; else changes[i] = 2; } // first case int first_result = 0; for(int i=0; i<n-1; i++){ if(i%2==0){ if(changes[i]!=0){ first_result++; i++; } } else{ if(changes[i]!=1){ first_result++; i++; } } } // second case int second_result = 0; for(int i=0; i<n-1; i++){ if(i%2==0){ if(changes[i]!=1){ second_result++; i++; } } else{ if(changes[i]!=0){ second_result++; i++; } } } // output cout<<min(first_result, second_result); return 0; } |