#include<iostream> #include<vector> using namespace std; bool rel(int a,int b,int c) { if(c) return a>b; return b>a; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> T(n); for (auto& i : T) cin >> i; int result1 = 0, result2 = 0; vector<int> A=T; for(int i=0 ; i<n-1 ; i++) { if(!rel(A[i],A[i+1],i%2)) { if(i%2==0)A[i+1]=1e9; else A[i+1]=-1e9; result1++; } } A=T; for(int i=0 ; i<n-1 ; i++) { if(!rel(A[i],A[i+1],(i+1)%2)) { if(i%2==1)A[i+1]=1e9; else A[i+1]=-1e9; result2++; } } cout<<min(result1,result2)<<endl; /*for (int i = 1; i < T.size() - 1; i += 2) { if (T[i] >= T[i - 1] || T[i] >= T[i + 1]) result1++; if (T[i] <= T[i - 1] || T[i] <= T[i + 1]) result2++; } if (n % 2 == 0 && T[n - 1] <= T[n - 2]) result1++; if (n % 2 == 0 && T[n - 1] >= T[n - 2]) result2++; int result3 = 0, result4 = 0; if (T[1] <= T[0]) result3++; if (T[1] >= T[0]) result4++; for (int i = 2; i < T.size() - 1; i += 2) { if (T[i] >= T[i - 1] || T[i] >= T[i + 1]) result3++; if (T[i] <= T[i - 1] || T[i] <= T[i + 1]) result4++; } if (n % 2 == 1 && T[n - 1] >= T[n - 2]) result3++; if (n % 2 == 1 && T[n - 1] <= T[n - 2]) result4++; cout << min(min(result1, result2), min(result4, result3)) << "\n";*/ 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 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include<iostream> #include<vector> using namespace std; bool rel(int a,int b,int c) { if(c) return a>b; return b>a; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> T(n); for (auto& i : T) cin >> i; int result1 = 0, result2 = 0; vector<int> A=T; for(int i=0 ; i<n-1 ; i++) { if(!rel(A[i],A[i+1],i%2)) { if(i%2==0)A[i+1]=1e9; else A[i+1]=-1e9; result1++; } } A=T; for(int i=0 ; i<n-1 ; i++) { if(!rel(A[i],A[i+1],(i+1)%2)) { if(i%2==1)A[i+1]=1e9; else A[i+1]=-1e9; result2++; } } cout<<min(result1,result2)<<endl; /*for (int i = 1; i < T.size() - 1; i += 2) { if (T[i] >= T[i - 1] || T[i] >= T[i + 1]) result1++; if (T[i] <= T[i - 1] || T[i] <= T[i + 1]) result2++; } if (n % 2 == 0 && T[n - 1] <= T[n - 2]) result1++; if (n % 2 == 0 && T[n - 1] >= T[n - 2]) result2++; int result3 = 0, result4 = 0; if (T[1] <= T[0]) result3++; if (T[1] >= T[0]) result4++; for (int i = 2; i < T.size() - 1; i += 2) { if (T[i] >= T[i - 1] || T[i] >= T[i + 1]) result3++; if (T[i] <= T[i - 1] || T[i] <= T[i + 1]) result4++; } if (n % 2 == 1 && T[n - 1] >= T[n - 2]) result3++; if (n % 2 == 1 && T[n - 1] <= T[n - 2]) result4++; cout << min(min(result1, result2), min(result4, result3)) << "\n";*/ return 0; } |