#include <bits/stdc++.h> using namespace std; const int mxn = 5e4+5; const int pINF = 1e9, nINF = -1e9; int arr[mxn]; int arr2[mxn]; int main(){ int n; scanf("%d",&n); for (int i = 0; i < n; ++i) { scanf("%d",&arr[i]); } memcpy(arr2, arr, sizeof arr); int sw1 = 0; for(int i = 1; i < n; ++i){ if(i&1){ if(arr[i] > arr[i-1]){ //fine }else{ //cout<<"swappingx 1 at "<<i<<endl; sw1++; arr[i] = pINF; } }else{ if(arr[i] < arr[i-1]){ //fine }else{ //cout<<"swappingy 1 at "<<i<<endl; sw1++; arr[i] = nINF; } } } int sw2 = 0; for(int i = 1; i < n; ++i){ if(!(i&1)){ if(arr2[i] > arr2[i-1]){ //fine }else{ //cout<<"swappingx 2 at "<<i<<endl; sw2++; arr2[i] = pINF; } }else{ if(arr2[i] < arr2[i-1]){ //fine }else{ //cout<<"swappingy 2 at "<<i<<endl; sw2++; arr2[i] = nINF; } } } //cout<<sw1<<" "<<sw2<<endl; printf("%d",min(sw1,sw2)); }
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 62 63 | #include <bits/stdc++.h> using namespace std; const int mxn = 5e4+5; const int pINF = 1e9, nINF = -1e9; int arr[mxn]; int arr2[mxn]; int main(){ int n; scanf("%d",&n); for (int i = 0; i < n; ++i) { scanf("%d",&arr[i]); } memcpy(arr2, arr, sizeof arr); int sw1 = 0; for(int i = 1; i < n; ++i){ if(i&1){ if(arr[i] > arr[i-1]){ //fine }else{ //cout<<"swappingx 1 at "<<i<<endl; sw1++; arr[i] = pINF; } }else{ if(arr[i] < arr[i-1]){ //fine }else{ //cout<<"swappingy 1 at "<<i<<endl; sw1++; arr[i] = nINF; } } } int sw2 = 0; for(int i = 1; i < n; ++i){ if(!(i&1)){ if(arr2[i] > arr2[i-1]){ //fine }else{ //cout<<"swappingx 2 at "<<i<<endl; sw2++; arr2[i] = pINF; } }else{ if(arr2[i] < arr2[i-1]){ //fine }else{ //cout<<"swappingy 2 at "<<i<<endl; sw2++; arr2[i] = nINF; } } } //cout<<sw1<<" "<<sw2<<endl; printf("%d",min(sw1,sw2)); } |