#include <iostream> using namespace std; int h[50005]; int hCopy[50005]; const int maxH = 1e6; const int minH = -1e6; int main() { int n; cin>>n; for(int i=0;i<n;i++) { cin>>h[i]; hCopy[i] = h[i]; } int result = 999999; int currentResult = 0; for(int i=0;i<n-1;i++) { if(i%2 == 0) { if(hCopy[i]<hCopy[i+1]) continue; currentResult++; hCopy[i+1] = maxH; } else { if(hCopy[i]>hCopy[i+1]) continue; currentResult++; hCopy[i+1] = minH; } } result = currentResult; currentResult = 0; for(int i=0;i<n-1;i++) { if(i%2 == 1) { if(h[i]<h[i+1]) continue; currentResult++; h[i+1] = maxH; } else { if(h[i]>h[i+1]) continue; currentResult++; h[i+1] = minH; } } result = min(result, currentResult); cout<<result<<"\n"; }
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 | #include <iostream> using namespace std; int h[50005]; int hCopy[50005]; const int maxH = 1e6; const int minH = -1e6; int main() { int n; cin>>n; for(int i=0;i<n;i++) { cin>>h[i]; hCopy[i] = h[i]; } int result = 999999; int currentResult = 0; for(int i=0;i<n-1;i++) { if(i%2 == 0) { if(hCopy[i]<hCopy[i+1]) continue; currentResult++; hCopy[i+1] = maxH; } else { if(hCopy[i]>hCopy[i+1]) continue; currentResult++; hCopy[i+1] = minH; } } result = currentResult; currentResult = 0; for(int i=0;i<n-1;i++) { if(i%2 == 1) { if(h[i]<h[i+1]) continue; currentResult++; h[i+1] = maxH; } else { if(h[i]>h[i+1]) continue; currentResult++; h[i+1] = minH; } } result = min(result, currentResult); cout<<result<<"\n"; } |